Difference between response.send and response.write in node js Difference between response.send and response.write in node js node.js node.js

Difference between response.send and response.write in node js


response.send(msg) is equal to response.write(msg);response.end();

Which means, send can only be called once, write can be called many times, but you must call end yourself.


I can't find response.send() in the docs, but I assume .send() will fill in and send the response so can only be called once, whereas .write() will just write the response, but you have to send it using response.end()

This means you can edit the headers using .write() because the response has not been sent yet.

EDIT :

response.send() is part of the restify Response API wrapper


res.send() is part of Express.js instead of pure Node.js.

Just an side observation. My app sometimes send back a very large Json object ( HighChart object that contains over 100k points). With res.send() sometimes it hangs and choke up the server, whereas res.write() and res.end() handle it just fine.

I also noticed a memory spike when res.send() is invoked.