0.0.5 • Published 8 years ago

restify4-utils v0.0.5

Weekly downloads
4
License
ISC
Repository
github
Last release
8 years ago

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

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago