I am a beginner to express.js
and I am trying to understand the difference between res.send
and res.write
?

- 2,971
- 3
- 21
- 26

- 878
- 1
- 9
- 15
-
2refer this link - https://stackoverflow.com/questions/21749590/difference-between-response-send-and-response-write-in-node-js – Rohit Waghela Jun 22 '17 at 06:59
6 Answers
res.send
res.send
is only in Express.js.- Performs many useful tasks for simple non-streaming responses.
- Ability to automatically assigns the
Content-Length
HTTP response header field. - Ability to provides automatic HEAD & HTTP cache freshness support.
- Practical explanation
res.send
can only be called once, since it is equivalent tores.write
+res.end()
- Example:
app.get('/user/:id', function (req, res) { res.send('OK'); });
For more details:
res.write
- Can be called multiple times to provide successive parts of the body.
- Example:
response.write('<html>'); response.write('<body>'); response.write('<h1>Hello, World!</h1>'); response.write('</body>'); response.write('</html>'); response.end();
For more details:

- 14,010
- 29
- 101
- 161

- 2,971
- 3
- 21
- 26
-
6To clarify on `res.write` little bit more, this is a method provided by `OutgoingMessage` class from node `http` module. Express.js `response` class inherits `OutgoingMessage` class. Below is the definition of `write` method: ```OutgoingMessage.prototype.write = function write(chunk, encoding, callback) { return write_(this, chunk, encoding, callback, false); }; ``` – Subrat May 10 '18 at 06:11
-
Another difference is, `send` automatically sets encoding to UTF-8 – santiago arizti May 12 '21 at 23:11
-
which has better performance, would write result in better first paint timing? – Muhammad Umer Feb 06 '22 at 02:44
res.send
is equivalent to res.write + res.end
So the key difference is res.send
can be called only once where as res.write
can be called multiple times followed by a res.end
.
But apart from that res.send
is part of Express. It can automatically detect the length of response header.
But there may be be a chance of memory spike with res.send(), in case of large files, our application hangs in between .

- 159
- 2
- 6
One of the most important differences not indicated in any of the answers are "draining".
The res.write
may return true or false. As of the documentation:
Returns true if the entire data was flushed successfully to the kernel buffer. Returns false if all or part of the data was queued in user memory. 'drain' will be emitted when the buffer is free again.
So, when doing res.write
, the caller should hold off writing until the drain event emits if the res.write
returned false.
All these are handled automatically in res.send
. The trade off is the buffering you will have to do when using the latter.

- 22,886
- 11
- 59
- 90
-
-
Express documentation has it all: https://expressjs.com/en/api.html#res – Charlie Jul 01 '20 at 17:15
I am also a beginner in this. But what I observed is that, if you write only res.write()
the page will be loading continuously until you write res.end()
. Whereas if you write res.send()
there is no need of res.end()
. res.send()
basically it does both res.write()
and res.end()
.

- 2,324
- 26
- 22
- 31

- 55
- 7
Suppose you have two line that needs to be shown up and you use res.send as
res.send("shows only First Line")
res.send("won't show second Line")
Then only first line will show up, whereas using res.write
you have flexibility to write multiple line such as
res.write("Shows first line")
res.write("Shows second line")
res.send()

- 33,893
- 13
- 69
- 83

- 1
- 1
res.send()
is equivalent to res.write()+ res.end()
.
basically, res.send
is for express and res.write+res.end()
is for bare metal node ways to send data.

- 1
- 1
- 3