42
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.

5 Answers 5

46

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.

5
  • 9
    writeHead writes the HTTP header (status code 200), end writes the body and closes the response. Did you check the API documentation? Jan 9, 2013 at 17:42
  • 2
    It's not a callback, it's listener to be bind to server's request event. Also, nodejs.org/api/http.html got all the answers to res.writeHead, res.write, res.end and other req / res stuff. Jan 9, 2013 at 19:15
  • 2
    But it still doesnt answer the question. The code you wrote still runs even without the writeHead method. Can you please explain why? Thanks
    – Brr Switch
    Jul 10, 2016 at 4:04
  • there should be a reason object in writeHeader method ,isn't it? ex:response.writeHead(200,{"context-type":"text/plain"}); response.write("this has no erros,okk"); response.end();
    – Kalanka
    Jan 30, 2017 at 11:44
  • 1
    This doesn't answer the OP question. Feb 19, 2018 at 6:58
21

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.

3
  • 1
    I just asumed that you have a reasonable idea of how HTTP works. Please feel free to ask further if you do not understand that. Oct 18, 2014 at 6:50
  • What will happen if I don't add end() but just writeHead() only
    – DunDev
    Dec 13, 2017 at 15:32
  • 1
    @DunDev Since you never tell the server to send the message, presumably the message is never sent. What exactly happens after the message is never sent depends on a variety of things, including if you've got timeouts set up (so if a message remains unreplied-to for too long, it's automatically errored out) and what options the client specifies.
    – anon
    May 30, 2018 at 23:16
4

response.writeHead(200) sends a response header to the request. The status code is a 3-digit HTTP status code, like 404.

This method must only be called once on a message and it must be called before response.end() is called.

If you call response.write() or response.end() before calling this, the implicit/mutable headers will be calculated and call this function for you.

3

As far as i know if you don't put the response.end() at the end then your web page will go on loading thus the response.end() is used to tell the server that the data has been loaded

0

The res.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. The res.end method is to make sure the response isn't returned before it might be ready, in case of nested code or otherwise.

The purpose of the options object is to make sure the page has a valid key and certificate before declaring that the page is encrypted under https.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.