Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 450
user3275959 Avatar asked Feb 13 '14 09:02

user3275959


People also ask

What is the difference between response send () and response write () in Express?

response. send() sends the response and closes the connection, whereas with response. write() you can send multiple responses. In this article, I will explain the difference between response.

What does Response write do node JS?

write() is called, it will send the buffered header information and the first chunk of the body to the client. The second time response. write() is called, Node. js assumes data will be streamed and sends the new data separately.

What is the difference between RES write and res end?

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 .

How do you send a response in node JS?

Methods to send response from server to client are:Using send() function. Using json() function.


Video Answer


2 Answers

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.

like image 108
Flying Fisher Avatar answered Oct 02 '22 18:10

Flying Fisher


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

like image 42
Jim Jeffries Avatar answered Oct 02 '22 19:10

Jim Jeffries