Default route in Express.js Default route in Express.js express express

Default route in Express.js


As stated here, you can add this middleware just after your routing logic:

   app.use(function(req, res){       res.send(404);   });

You might find this answer also useful.

Of course, you need to adapt the res.send() part to meet your needs.


Add this route at the after of all your previous routes

app.get('*',function (req, res) {        res.redirect('/');    });

This will redirect any route not handled to the index "/"


With the newer version of express I would suggest using res.sendStatus as res.send has been deprecated.

Express v3

app.use(function(req, res){   res.send(404);});

Express v4

app.use(function(req, res){   res.sendStatus(404);});