Express 4.14 - How to send status 200 using a special message?

How can I send status and message in express 4.14?

For: res.sendStatus (200);

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

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

Mistake:

Error: Failed to set headers after sending them.

If I am this :

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

Mistake:

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

Any ideas?

+8
source share
2 answers

You can use:

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

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

reject for sending status directly inside send.

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

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

+16
source share

You should not get the latest error if you use this exact code:

 res.status(200).send('Success 1') 

I assume that you are not using the string "Success 1", but instead a digital variable or value:

 let value = 123; res.status(200).send(value); 

This will cause a warning. Instead, make sure value compressed:

 let value = 123; res.status(200).send(String(value)); 
+3
source share

All Articles