You need to define a middleware to handle it and that middleware needs to be defined after all the other routes and middlewares have been specified already.

The logic is that this middleware will only be executed if the request cannot match with any other middleware/route.

app.use((req, res, next) => {
  res.status(404).send("Not found");
});

And if you don't want to provide a body then.

app.use((req, res, next) => {
  res.sendStatus(404);
});