Reputation: 13846
This doesn't show any response to an HTTP request, just hangs:
function middleware(req, res, next) {
to_end(res).NotFound();
return next();
}
This works:
function middleware(req, res, next) {
to_end(res).NotFound();
res.send(); // <----------------------------------------------
return next();
}
I'm using restify with this simple function:
export const to_end = res => {
return {
NotFound: (entity = 'Entity') => res.json(404, {
error: 'NotFound', error_message: `${entity} not found`
})
}
}
Upvotes: 0
Views: 800
Reputation: 5738
I'm not so good at ES6 but I guess that it may be the javascript's context problem (I mean the this
variable).
In your case, res.json()
may throw res is not defined
(I guess) since the this
at that moment is the context of export const to_end ...
(thanks to =>
's magic), which does not know what is res
.
Sorry for my terribly bad explanation :(. Read this to understand more about this
.
We can make things simpler:
module.exports = function(res) {
return {
NotFound: function() {
res.json(404, {
error: 'NotFound',
error_message: `${entity} not found`
});
}
}
};
P/S: Not tested yet. Good luck! :)
Upvotes: 0