server error sent to client in express

In the following node.js server code, since "ABCDE" is not a defined a variable, error is thrown when '/upload' is requested. What confused me is that, the error stack trace printed on the console at server side is sent back to the client, which is unsafe.

How can I prevent that, other than catch that error?

var express = require('express');
var app = express();

app.use(function (err, req, res, next) {
    res.send(500, 'Something broke!');
});
app.post('/upload', function (req, res) {
    console.log(ABCDE);
});
app.listen(3000);

You already have the answer in your question. You need error-handling middleware (app.use a function with an arity of 4) to handle the error.

You just need to add the error-handling middleware after the router. Your example puts the the error handler above the the router.

Either move the app.use(function (err, req, res, next)) to the bottom of your code or insert

app.use(app.router);

above the error handler.

Refer to the documentation for more information about error handlers.