node.js - Node express 404 error HTTP status code -


i have stripped down node express application bare minimum. not define routes anymore. when go route, e.g. localhost:3000/sdfsdf, expected output:

{"message":"not found","error":{"status":404}}

the http status code of response 200. expect 404. missing?

var express = require('express'); var path = require('path');  var app = express(); app.use(express.static(path.join(__dirname, 'public')));  // catch 404 , forward 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; 

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

you override res.statuscode 'error'. change to

res.status(err.status || 500); res.json({ 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -