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?
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');
Updated Answer for Express 4.x
Rather than using res.send(404)
as in old versions of Express, the new method is:
res.sendStatus(404);
Express will send a very basic 404 response with "Not Found" text:
HTTP/1.1 404 Not Found
X-Powered-By: Express
Vary: Origin
Content-Type: text/plain; charset=utf-8
Content-Length: 9
ETag: W/"9-nR6tc+Z4+i9RpwqTOwvwFw"
Date: Fri, 23 Oct 2015 20:08:19 GMT
Connection: keep-alive
Not Found