How to programmatically send a 404 response with Express/Node?

Randomblue picture Randomblue · Dec 6, 2011 · Viewed 152.2k times · Source

I want to simulate a 404 error on my Express/Node server. How can I do that?

Answer

Drew Noakes picture Drew Noakes · May 8, 2013

Since Express 4.0, there's a dedicated sendStatus function:

res.sendStatus(404);

If you're using an earlier version of Express, use the status function instead.

res.status(404).send('Not found');