Strengthen the normal handler function
- Support for the promise handler or the non-promise handler
- Non-promise handler are converted to promise handler
- Support handler return null/string as response output
- Support timeout
Handler returns:
- undefinde ---- do nothing
- null/empty string ---- 204 response
- string ---- string as response
- other ----- reject
const http = require('http');
const strengthan = require('strong-handler');
function handler(req, res) {
if (req.url === '/timeout') {
} else if (req.url === '/null') {
return null;
} else if (req.url === '/error') {
throw new Error('fail');
} else {
return 'hello world';
}
}
var strongHandler = strengthan(handler, 5000);
function httpHandler(req, res) {
strongHandler(req, res).catch((e) => {
console.log(e);
res.statusCode = 500;
res.end('Server Error');
});
};
http.createServer(httpHandler).listen(3000);