restify-await-promise
Converts restify routes to support async/await and returned promises. Works with Restify 4.x through 6.x. May work with Restify 7+.
#Supported Restify Versions
- Fully Supported
- Probably Works
- Partially Compatible
Usage
const restify = require('restify');
const restifyPromise = require('restify-await-promise');
const server = restify.createServer({
name: 'myapp',
version: '1.0.0'
});
const alwaysBlameTheUserErrorTransformer = {
transform: function( exceptionThrownByRoute ){
exceptionThrownByRoute.statusCode = 400;
return exceptionThrownByRoute;
}
}
const options = {
logger: yourLogger,
errorTransformer: alwaysBlameTheUserErrorTransformer
};
restifyPromise.install( server, options );
server.get('/lookup/:name', async function (req) {
return await SomePromise.work( req.parms.name );
});
server.get('/echo/:name', function (req) {
const params = req.params;
return Promise.resolve( { params } );
});
server.get('/echo2/:name', function (req, res, next) {
res.send(req.params);
next();
});
server.get('/seek/:enlightenment', restifyPromise.asyncConditionalHandler([
{
version: "1.0.0",
handler: async function (req) {
const result = await searchFor( req.parms.enlightenment );
return result;
}
}
]));
server.listen(8080, function () {
console.log('%s listening at %s', server.name, server.url);
});
Installation
$ npm install --save restify-await-promise
Bugs
See https://github.com/PhinCo/restify-await-promise/issues.