1

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;

1 Answer 1

2
res.status(err.status || 500);
res.status('error').json({

you finally override res.statuscode with 'error'. change to

res.status(err.status || 500);
res.json({
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a bunch. I did not see that. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.