I use express in node.js,and i want to custom 404 and 500 error page.
i write the code like below:
// Handle 404
app.use(function (req, res) {
res.send('404: Page not Found', 404);
});
// Handle 500
app.use(function (error, req, res, next) {
res.send('500: Internal Server Error', 500);
console.log(error);
});
it's right if 404 or error in background,but when there is a error in ejs(in a view),eg null or undefined ,it will show the detail error info..
such as:
Express
500 ReferenceError:
at eval (eval at (D:\node_workspace\study\node_modules\ejs\lib\ejs.js:237:14), :29:1117)
at eval (eval at (D:\node_workspace\study\node_modules\ejs\lib\ejs.js:237:14), :29:1593)
etc..
i really don't want to show such error page to users.. so,how can i config to avoid this?
U can use something like this
if (app.settings.env === 'production') {
app.error(function(err, req, res) {
res.render('500.jade', {
status: 500,
locals: {
error: error
}
});
});
}
And then set the custom message in the jade file like:
h2 Error
p Something went wrong with the application.
h3 Error Details
pre #{error}
Hope this helps