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

Javascriptnode.jsRestify

Javascript Problem Overview


I have written a small API which uses the Node js "restify" framework. This API receives a request (actually anything after "/") and then send that request to another server. Get the response back from server and passes the response back to original source of request. For this API I am using both restify server and client.

Below is that API code for better understanding.

var apiServer = require('apiServer');
apiServer.start();

var restify = require('restify');
var assert = require('assert');

function onRequest(request, response, next)
{
    var client = restify.createStringClient({ 
        url: 'http://example.com'
    });
        
    client.get('/' + request.params[0], function(err, req, res, data) {
        assert.ifError(err);

        response.setHeader('Content-Type', 'text/html');
        response.writeHead(res.statusCode);
        response.write(data);
        response.end();
    });
    next();
}

function start()
{
    var server = restify.createServer();
    server.get(/^\/(.*)/, onRequest);
    server.listen(8888);

    console.log("Server has started.");
}

exports.start = start;

Now I need to know the difference between response.write and response.send of Node.js. Because with response.write I can set header and write in it but it is not possible to do anything with headers when I use response.send. When I use response.send with setHeader() or writeHeader() I get this error:

http.js:691
throw new Error('Can't set headers after they are sent.');
^
Error: Can't set headers after they are sent.

There is also another thing. With response.send() I get the complete HTML output on the screen like:

<!DOCTYPE html>\n<html>\n\t<head></head></html> ..... "bla bla bla"

But with response.write I do not get the html on screen but only the text "bla bla bla".

It would be great if someone can explain me the differences.

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - Javascript

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

Solution 3 - Javascript

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.

Solution 4 - Javascript

I was trying to send huge text data(295mb) over http using res.send(data) and res.write(data). I noticed that res.send(data) is slower than res.write(data). I observed following things.

res.send(data): it can be called only once and it sends data in chunk of some buffer size to client and then again comes back and sends another chunk of buffer size so there is a lot of back and forth http communication.

res.write(data): It can be called multiple times followed by res.end() and It creates buffer size based on whole data and sends over http so it would be faster in case of huge amount of data.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser3275959View Question on Stackoverflow
Solution 1 - JavascriptFlying FisherView Answer on Stackoverflow
Solution 2 - JavascriptJim JeffriesView Answer on Stackoverflow
Solution 3 - JavascriptGäng TianView Answer on Stackoverflow
Solution 4 - JavascriptShashi3456643View Answer on Stackoverflow