The http. createServer method in node. js is described as follows:
Method description:
This function is used to create an HTTP server and use requestListener as the listener function for request events.
Syntax:
Copy codeThe Code is as follows:
Http. createServer ([requestListener])
Because this method belongs to the http module, the http module (var http = require ("http") must be introduced before use "))
Receiving parameters:
The requestListener request processing function is automatically added to the request event. The function passes two parameters:
The req request object. If you want to know the attributes of req, you can view "http. request attribute integration ".
Res response object. The response to be made after receiving the request. For details about attributes of res, refer to "http. response attribute integration ".
Example:
In this example, res specifies the response header, and the response body content is node. js. end with end.
Finally, call the listen function to listen to port 3000.
Copy codeThe Code is as follows:
Var http = require ('http ');
Http. createServer (function (req, res ){
Res. writeHead (200, {'content-type': 'text/html '});
Res. write ('Res. end ('<p> Hello World </p> ');
}). Listen (3000 );
Source code:
Copy codeThe Code is as follows:
Exports. createServer = function (requestListener ){
Return new Server (requestListener );
};