HTTP module, which encapsulates an efficient HTTP server and a recommended HTTP client
Http.server is an event-based HTTP server
Http.request is an HTTP client tool that the user sends a request to the server.
HTTP Server
(1) HTTP. Server implementation, provides a set of low-level API, only flow control and simple parsing.
Request when a client request arrives, the event is triggered, providing two parameters, namely HTTP. An instance of Serverrequest and http.serverresponse that represents the request and the corresponding information.
varHttp=require ('http');varServer=Newhttp. Server (); Server.on ('Request', Function (req,res) {Res.writehead ( $,{'Content-type':'text/html'}); Res.write (''); Res.end ("<p>i ' m request</p>");}); Server.listen ( the);
HTTP provides a shortcut to Http.createserver ([Requestlistner])
varHttp=require ('http')varServer=http.createserver (function (req,res) {Res.writehead ( $,{'Content-type':'text/html'}); Res.write (''); Res.end ('<p>this Use createserver</p>');}); Server.listen ( the); Server.on ('Close', function () {Console.log ('server is close');}) Console.log ('HTTP Server is listening at port.');
http. Serverresponse
It is by HTTP. The server's response event is sent.
There are three main functions
Response.writehead (Statuscode,[headers])
Response.Write (Data,[encoding])
Response.End (data,[endcoding]), the function must be called once, or the client will always be in a wait state.
node. JS Basic Learning Note 3-http