Set CORS headers and answer preflight requests in begin.
var server = require('@nodedk/server')
server({
begin: function (state) {
state.cors({ origin: 'https://example.com' })
if (state.req.method === 'OPTIONS') {
state.res.statusCode = 204
return null
}
},
routes: [{ paths: '/', handler: () => ({ ok: true }) }]
})
Options: origin, methods, and headers. Defaults are *, GET, POST, HEAD, OPTIONS, and content-type, authorization. The helper sets headers only.
Next: Basic auth →