express js 4 how to serve json results without rendering any views /css
If you're making any calls to res.render
such as in an error handler that are generated by the 'express generate', then you'll see the error you described. For a json API service you probably don't need to render anything so just don't call render()
, instead call res.send()
with the status res.status set to 404 or 500.
So basically, replace this:
app.use(function(err, req, res, next) { res.render('error', { message: err.message, error: err });});
with this:
app.use(function(err, req, res, next){ res.status(err.status || 500); res.send({ message: err.message, error: err }); return;});