I have stripped down my node Express application to the bare minimum. I do not even define routes anymore. When I go to any route, e.g. localhost:3000/sdfsdf, I get the expected output:
{"message":"Not Found","error":{"status":404}}
The HTTP status code of the response however is 200. I expect it to be 404. What am I missing?
var express = require('express');
var path = require('path');
var app = express();
app.use(express.static(path.join(__dirname, 'public')));
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// development error handler
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
//res.set('Content-Type', 'application/json');
res.status(err.status || 500);
res.status('error').json({
message: err.message,
error: err
});
});
}
// production error handler
app.use(function(err, req, res, next) {
res.set('Content-Type', 'application/json');
res.status(err.status || 500);
res.status('error').json({
message: err.message,
error: {'env': 'prod'}
});
});
module.exports = app;