Method description:
This function is used to create an HTTP server and use requestListener as the listener for the request event.
grammar:
The code copy is as follows:
http.createServer([requestListener])
Since this method belongs to the http module, the http module needs to be introduced before use (var http= require("http") )
Receive parameters:
The requestListener request handler function is automatically added to the request event. The function passes two parameters:
Req request object. If you want to know what properties req has, you can check "http.request attribute integration".
res response object, the response to be made after receiving the request. If you want to know what properties res have, you can check "http.response attribute integration".
example:
In the example, res specifies the response header, the response body content is node.js, and ends with end.
Finally, call the listen function and listen to port 3000.
The code copy is as follows:
var http = require('http');
http.createServer(function(req, res){
res.writeHead(200, {'Content-type' : 'text/html'});
res.write('<h1>Node.js</h1>');
res.end('<p>Hello World</p>');
}).listen(3000);
Source code:
The code copy is as follows:
exports.createServer = function(requestListener) {
return new Server(requestListener);
};