smallorange-connect
v1.0.1
Published
Emulates express/restify handlers execution and returns the results via callback.
Downloads
4
Readme
Small Orange Connect
It emulates express/restify middlewares execution and returns the results via callback.
Sample
const Connect = require('smallorange-connect');
const req = {}; // any req object
const res = {}; // allow override methods
const callback = console.log;
const connect = new Connect(req, res, callback);
const middlewares = [
(req, res, next) => {
req.someAttr = 1;
next();
},
(req, res, next) => {
req.someAttr = 2;
next();
},
[ // nested middleware
(req, res, next) => {
req.someAttr = 2;
res.header('content-type', 'text/html');
res.status(200);
res.end(req.someAttr);
},
]
];
connect.executeHandlers(middlewares);
// once execute, will log via callback
{
headers: {
'content-type': 'text/html'
},
statusCode: 200,
body: 2
}
// or with errors
const middlewares = [
(req, res, next) => {
req.someAttr = 2;
next('some error');
}
];
connect.executeHandlers(middlewares);
// once execute, will log via callback
{
statusCode: 500,
message: 'some error'
}