This article mainly introduces node. http. the createServer method is described in this article. createServer method description, syntax, receiving parameters, use instances and implementation source code. For more information, see
Method description:
This function is used to create an HTTP server and use requestListener as the listener function for request events.
Syntax:
The 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.
The Code is as follows:
Var http = require ('http ');
Http. createServer (function (req, res ){
Res. writeHead (200, {'content-type': 'text/html '});
Res. write ('node. js ');
Res. end ('
Hello World
');
}). Listen (3000 );
Source code:
The Code is as follows:
Exports. createServer = function (requestListener ){
Return new Server (requestListener );
};