@gilbertco/restify-async-wrap

0.1.2 • Public • Published

Node Restify Async Wrap

npm i -S @gilbertco/restify-async-wrap

The only issue with using ES7 Async functions within Restify is dealing with un-caught exceptions. This can cause the route to hang, the client then timing out without error notification, which can also lead to possible denial of service attacks by blocking the Node.js event loop.

This module is mentioned within the following blog entry: https://blog.gilbertco.xyz/node-js-cleaner-api-routes-with-async-await/

CI Build Status

CircleCI

Basic Example

const restifyAsyncWrap = require('@gilbertco/restify-async-wrap');
const restify = require('restify');

const server = restify.createServer();

// Default Restify Error Handling
server.get('/', restifyAsyncWrap(async () => {
  throw new Error('Caught error and used default 500 Restify error handling method.');
}));

// Custom Error Handling
server.get('/test', restifyAsyncWrap(async () => {
  throw new Error('Caught error and returned 404.');
}), customErrorHandler);

function customErrorHandler(request, response, next, error) {
  response.send(404, {error});
  return next();
}

Readme

Keywords

Package Sidebar

Install

npm i @gilbertco/restify-async-wrap

Weekly Downloads

73

Version

0.1.2

License

MIT

Last publish

Collaborators

  • kgilbertco