routes
Introduction
Every light route must export the route
function with a callback function inside (which can be an async function) which is responsible for handling request. The route will have access to req
(IncomingMessage) and res
(ServerResponse) as parameters (these may vary between different environments).
Req/Res
The req
and res
variables are standard IncomingMessage and ServerResponse objects. In some environments, properties may be added or missing. For example, in AWS/Netlify mode, the req
object will be missing some request and response properties because they were casted from event
and context
. You should only use properties that you know are going to be available in your production environment.
Return
If you choose to return
from the function (instead of send
ing), you must return a valid response. Whatever value that is returned will be serialized and sent as a response. You can choose to return JSON, String, or any other type that micro supports under the hood.
Send
Additionally you may use micro
's send function to return different kinds of responses. The send function can be imported from light and used anywhere in the handler. See micro's documentation for more details.
Last updated