restify4-utils

0.0.5 • Public • Published

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 :)

travis npm standard

Installation:

npm i restify4-utils --save

Utils

The following utils are provided:

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'
});

Readme

Keywords

none

Package Sidebar

Install

npm i restify4-utils

Weekly Downloads

0

Version

0.0.5

License

ISC

Last publish

Collaborators

  • max.nachlinger