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

I wrote a small API that uses the Node js "restify" framework. This API receives the request (in fact, something after the "/"), and then sends the request to another server. Get the response from the server and send the response back to the original request source. For this API, I use both server and client recovery.

Below is the API code for a 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 from Node.js. Because with response.write I can set the title and write in it, but when using response.send I can do nothing with the headers. 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 another. With response.send() I get the full HTML output on the screen, for example:

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

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

It would be great if someone could explain the differences to me.

+45
javascript restify
Feb 13 '14 at 9:11
source share
4 answers

I cannot find response.send() in docs , but I assume that .send() populate and send the answer, so that only call once, while .write() will just write the answer, but you have to send it with using response.end()

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

EDIT :

response.send() is part of updating the response API shell

+28
Feb 13 '14 at 9:17
source share

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

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

+53
Feb 13 '14 at 9:17
source share

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

Just lateral observation. My application sometimes sends back a very large Json object ( HighChart , which contains more than 100 thousand points). With res.send() sometimes it hangs and suffocates from the server, while res.write() and res.end() handle it just fine.

I also noticed a burst of memory when calling res.send() .

+26
Sep 30 '14 at 18:40
source share

I tried to send huge text data (295mb) via http using res.send (data) and res.write (data). I noticed that res.send (data) is slower than res.write (data). I noticed the following things.

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

res.write (data): It can be called several times, followed by res.end (), and creates a buffer size based on integer data and sends via http so that it is faster in case of a huge amount of data.

+1
Dec 17 '16 at 19:05
source share



All Articles