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.


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);
});

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 "/"