Set hooks on the server to run them across requests.
var server = require('@nodedk/server')
server({
routes: [{ paths: '/', handler: () => 'Hello!' }],
begin: function (state) {
state.data.started = Date.now()
},
before: function (state) {
state.res.setHeader('x-app', 'example')
},
after: function (state) {
if (!state.res.headersSent) {
state.res.setHeader('x-request-id', state.req.id)
}
}
})
begin runs before routing and body parsing. before runs for matched routes. after runs before the normal response is sent. Static files may have already sent headers.
Next: Route hooks →