restify4-utils
v0.0.5
Published
A set of utils I use a lot when using restify 4.x on projects.
Downloads
1
Readme
restify4-utils
A set of utils I use a lot when using restify 4.x on projects. If I'm the only one who ever uses this, then I'll have succeeded :)
Installation:
npm i restify4-utils --save
Utils
The following utils are provided:
- A Bunyan logger which simply logs things to
process.stdout
, you can also add additional logging streams / serializers etc to this logger if you need them (see [Extending the Bunyan logger] (#extending-the-bunyan-logger) below). - A Bunyan audit logger which uses the
bunyanLogger
above and prints timing info for server calls. You can use it as it is, or add it to you server instance via addBunyanAuditLogger. - A formatter wrapper to keep error details from going out in server responses.
- A
server.on('uncaughtException')
handler which logs the exception and sends a vanilla 500 response. - A simple healthcheck route.
Basic Example
'use strict'
const restify = require('restify')
const restify4Utils = require('restify4-utils')
const port = 8080
const logger = restify4Utils.bunyanLogger
const server = restify.createServer({
log: logger
})
restify4Utils.excludeErrorsFromResponse(server)
server.use(restify.acceptParser(server.acceptable))
server.use(restify.gzipResponse())
server.use(restify.authorizationParser())
server.use(restify.requestLogger())
restify4Utils.handleUncaughtExceptions(server)
restify4Utils.addBunyanAuditLogger(server)
restify4Utils.addHealthCheck(server)
server.listen(port, (err) => {
if (err) {
logger.error('Unable to start server', err.stack || err)
process.exit(1)
}
logger.info(`Server listening on port: ${port}`)
})
server.get({
description: 'Simple GET which returns next(err)',
path: '/error/nodejs/next'
}, (req, res, next) => {
// simulates async work
setTimeout(() => {
// goes through the formatters to res.send
next(new Error('Test error'))
}, 500)
})
server.get({
description: 'Simple GET triggering an uncaughtException',
path: '/error/nodejs/throw'
}, () => {
// simulates async work
setTimeout(() => {
// handled by server.on('uncaughtException')
throw new Error('Test error')
}, 500)
})
This example is available here.
Once that server is running, try these cURLs:
curl "http://localhost:8080/error/nodejs/next" -verbose
curl "http://localhost:8080/error/nodejs/throw" -verbose
curl "http://localhost:8080/healthcheck" -verbose
Extending the Bunyan logger
const restify4Utils = require('restify4-utils')
const logger = restify4Utils.bunyanLogger
// this is just an instance of a Bunyan logger, so go crazy :)
// hey look, I"m changing the logger
logger.addStream({
type: 'stream',
stream: process.stderr,
closeOnExit: false,
level: 'error'
});