Express 4.14 - How to send 200 status with a custom message?

laukok picture laukok · Jul 2, 2016 · Viewed 38.4k times · Source

How can I send status and message in express 4.14?

For: res.sendStatus(200);

I get OK on my browser but I want it to display a custom message such as: Success 1

res.sendStatus(200);
res.send('Success 1');

Error:

Error: Can't set headers after they are sent.

If I do this:

res.status(200).send(1);

Error:

express deprecated res.send(status): Use res.sendStatus(status) instead

Any ideas?

Answer

Dima Grossman picture Dima Grossman · Jul 2, 2016

You can use:

res.status(200).send('some text');

if you want to pass number to the send method, convert it to string first to avoid deprecation error message.

the deprecation is for sending status directly inside send.

res.send(200) // <- is deprecated

BTW - the default status is 200, so you can simply use res.send('Success 1'). Use .status() only for other status codes