Search code examples
javascriptnode.jsexpresshttp-status-codes

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


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


Solution

  • 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');