Instructions for the http.createServer method in node.js
- 2020-05-05 10:51:48
- OfStack
method description:
This function is used to create an HTTP server and to use requestListener as a listener for request events.
syntax:
http.createServer([requestListener])
Since this method belongs to the http module, it is necessary to introduce the http module (var http= require(" http "))
before using itreceive parameters:
The requestListener request handler, which is automatically added to the request event, passes two arguments:
req request object to see what properties req has, see "http.request attribute integration".
res response object, the response to be made upon receipt of a request. To see what attributes res has, see "http.response attribute integration."
example:
In the example, res specifies the response header, and the response body content is node.js, ending with end.
Finally, the listen function is called to listen on port 3000.
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:
exports.createServer = function(requestListener) {
return new Server(requestListener);
};