A browsed website on the browser is the combination of numerous requests and the corresponding responses. An html file is first requested by the browser, while parsing the arrived html file as response, it discovers other required files such as image files, JavaScript files. At that point, the browsers sends the request for each of the required files and receive the comparing file as a response and this communication can be seen in any browsers, in order to see this open the developer tool or its substitutes and go to the network sections and open the network section of the web developer tools of any browser and look at the http action.
In order to create the Node.js HTTP server there are various functions as follows.
http.createServer([requestListener]) -
The HTTP server can be created using this function and the server created do not accept any request but returns an object of the http.server class. The fucntion accepts only on e argument which listens the event request included with request and responds parameters which are supplied by Node.js
The request object is used to get the current http request information and the responds object is used to send for the current http request.
server.listen([port][, hostname][, backlog][, callback]) -
The function accepts the request by listening to the given port and host by client, the function have four arguments as follows.
port - The http server accepts the request on the given port number.
hostname - The hostname on which the http server accepts the client request and default hostname is Localhost.
backlog - The number client requests can be queued by server by using the backlog argument and the default backlog value would be 511
callback - The callback argument is the optional argument and is the listener to the listening event.
When the post and hostname not given the function listens to the path given and is known as the Unix socket which have only two arguments in which the first argument is the path and the second argument is the optional argument which listen to the listening event.
server.close([callback]) -
No other new connections are accepted by utilizing the server.close() function and the server gets closed only after all the existing servers are closed.
The function have only one argument “
callback” which is an optional argument and is executed only after the close event emitted successfully.
server.setTimeout(msecs, callback) -
A timeout value is given to the socket created by server by using this function. When the socket of client is idle for the given time then the timeout event is emitted by the server. The function have two arguments as follows.
msecs - The given time should be in milliseconds in which the socket needs to time out.
callback - The callback argument is the optional argument.
The listener must and should handle the timeout socket when there is a listener for the timeout event, if there is no timeout event the timeout socket get demolished.