Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

response.writeHead and response.end in NodeJs

Tags:

node.js

var https = require('https'); var fs = require('fs');  var options = {   key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),   cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem') };  https.createServer(options, function (req, res) {   res.writeHead(200);   res.end("hello world\n"); }).listen(8000); 
  1. Can anyone explain me why do we call the writeHead and end method in createServer method.
  2. What is the main purpose of options object passed in createServer method.
like image 265
Kevin Avatar asked Jan 09 '13 17:01

Kevin


People also ask

How do you end a response in node JS?

end() method ends the current response process. This method is used to quickly end the response without any data.

What does writeHead do in node JS?

writeHead method is for returning a status code to the browser, and the browser will throw an error if it is a client-side status code or server-side status code.

What is the difference between setHeader and writeHead?

The difference between setHeader and writeHead is that setHeader sets one header at a time, and you can use as many setHeader methods as you need before you send the response.

How many arguments writeHead () method takes?

writeHead(statusCode[, statusMessage][, headers]); Parameters: It accepts three parameters as mentioned above and described below: statusCode <number>: It accepts the status codes that are of number type.


2 Answers

Those calls to writeHead and end are not being done in the createServermethod, but rather in a callback.

It's a bit easier to see if you split out the callback into a separate function:

function handleRequest(req, res) {   res.writeHead(200);   res.end("hello world\n"); }  https.createServer(options, handleRequest).listen(8000); 

So here we define a handleRequest function and then pass that into the createServer call. Now whenever the node.js server we created receives an incoming request, it will invoke our handleRequest method.

This pattern is very common in JavaScript and is core to node.js' asynchronous event handling.

like image 88
Frank van Puffelen Avatar answered Sep 19 '22 21:09

Frank van Puffelen


In your code, the writeHead() is called to write the header of the response, that the application will serve to the client. The end() method both sends the content of the response to the client and signals to the server that the response (header and content) has been sent completely. If you are still going to send anything else, you should call write() method of res response object instead.

The options JSON object is a modifier that you may use, to override the default behaviour of the createServer() method. In your code's case:
+ key: Private key to use for SSL (default is null)
+ cert: Public x509 certificate to use (default is null)

You can find more in this section of the Node.js API doc about the response.writeHead() method.
You can find more in this section of the Node.js API doc about the https.createServer() method.

like image 33
Gui Imamura Avatar answered Sep 20 '22 21:09

Gui Imamura