Define server options in app.js:
var server = require('@nodedk/server')
server({
// Defaults: NODE_ENV or 'production', APP_PORT or 3000,
// and APP_WORKERS or 1
mode: 'development',
port: 3000, // 0 picks a free port
workers: 1, // 0 uses available CPUs
root: __dirname, // Defaults to process.cwd()
base: 'app', // Relative to root
static: 'app/assets', // Relative to root
autoroute: ['pages', 'actions'], // Used when routes is omitted
revision: '', // Default: empty in development, root folder name otherwise
// Languages and translations, merged with validation messages
lang: 'en',
languages: ['en'],
locales: {
en: { home: { title: 'Welcome' } }
},
// CSS and JavaScript file lists
bundle: { css: [], js: [] },
// Services available through state.plugins
plugins: {
log: function (message) {
console.log(message)
}
},
routes: [
{
paths: '/',
handler: function (state) {
return state.t('home.title')
}
}
],
// Before request checks and routing
begin: function (state) {
state.data.started = Date.now()
},
// Before each matched route chain
before: function (state) {
state.res.setHeader('x-app', 'example')
},
// After the normal route chain
after: function (state) {
if (!state.res.headersSent) {
state.res.setHeader('x-request-id', state.req.id)
}
},
// When request handling throws
error: function (state) {
console.error(state.err)
state.res.statusCode = 500
state.result = 'Please try again'
},
// After response handling; not awaited
done: function (state) {
var ms = Date.now() - state.data.started
state.plugins.log('Request finished in ' + ms + 'ms')
},
// When the server starts listening
ready: function ({ port, mode, pid, ms }) {
console.log({ port, mode, pid, ms })
}
})
See Route options for a complete route example and the server reference for more details.
Next: Route options →