Reputation: 41
Is node http request actually fired after req.end or after http.request ?
Context: I am using node js http module and wanted to understand what happens between:
Can node open socket and start dns look up before req.end() Or req.end() is to just suggest that no more data needs to be sent ?
From documentation "With http.request() one must always call req.end() to signify the end of the request - even if there is no data being written to the request body." I am not sure what to make out of this ?
Upvotes: 1
Views: 76
Reputation: 140
Is node http request actually fired after req.end or after http.request ?
http.request() returns an instance of the http.ClientRequest class and the ClientRequest instance is a writable stream. Therefore, the request will be fired after req.end()
Can node open socket and start dns look up before req.end() Or req.end() is to just suggest that no more data needs to be sent ?
The answer for your question is no, the socket will be created after you sent the request, as mentioned before when you use req.end().
From documentation "With http.request() one must always call req.end() to signify the end of the request - even if there is no data being written to the request body." I am not sure what to make out of this ?
I think you should try to understand a little more about node streams. When you invoke the request method from http, it returns a writable stream, that means the request is "available" to be written. When you do the .end() you are telling to that stream that no more writing is needed, in other words, you don't need to build the request anymore and so the request is sent.
Upvotes: 0