Basic routing
In general, routing in Lunatic is as follows:
app.METHOD(PATH?, HANDLER)
Where:
- METHOD - HTTP method of request (
GET
,PUT
...) - PATH (optional) - request path (
/
,/api
...) - HANDLER - either Router or RequestHandler
Request handler
Request handler (of type RequestHandler
) is a function that accepts 3 arguments:
req
:Request
- incoming HTTP requestres
:Response
- server HTTP responsenext
:NextHandler
- next RequestHandler, which can be called in the current one
Examples
Here are simple routing examples. You can add them in your Hello World app:
POST
app.post('/', (_req, res) => {
res.status(200).json({ message: 'POST request' })
})
PUT
app.put('/', (_req, res) => {
res.status(200).json({ message: 'PUT request' })
})
DELETE
app.delete('/', (_req, res) => {
res.status(200).json({ message: 'DELETE request' })
})
tip
For more details about routing, see Routing.