< a > HTTP basic createserver () Related events Introduction
1. Creating an HTTP server
Server = Http.createserver ([Requestlistener])
The following introduction uses server objects created by server
Requestlistener is a callback function (Request,response) {...}
Parameter description: The request client sends requests to the server, the response server responds to the client's request, and sends the content to the client.
If you do not use the callback function inside Createserver (), you can create a server object request event (triggered when a client request is received) by listening to the method.
Server.on (' request ',function(request,response) { //})
2. Listen Listening port
Server.listen (Prot,[host],[backlog],[callback])
Parameter description:
The port parameter value specifies the port number to listen on, and a value of 0 o'clock will randomly assign a port number to the HTTP server;
The host parameter is used to specify the address to listen to, and if omitted, the server listens for client links from any IPV4 address;
The backlog parameter value is an integer that specifies the maximum number of client connections that are in the waiting queue, and once this integer value is exceeded, the HTTP server will start rejecting the default maximum parameter value of 511 from the new client connection;
When the HTTP server is specified to listen for the address and port, the server will immediately start listening to the client connection from that address and port, then trigger the server's listening event, The callback parameter of the Listen method can be used to specify the callback function that is called when the listening event is triggered, or the
Server.on (' Listening ',function() { //})
3. You can use the Close method of the HTTP server to close the server by using the following method
Server.close ();
When the server is shut down, triggering the server's close event, you can specify the processing that needs to be performed when the server is shut down by listening for the event and specifying the callback function's method.
Server.on (' Close ',function() { // callback function });
4. When specifying the address and port number to be monitored on the HTTP server, if the address and port number are occupied, an error code "Eaddrinuse" (indicating the address used for listening and the port number is occupied) is generated, and the HTTP server object error event is triggered.
Server.on (' ERROR ',function(e) { if(e.code = = = ' Eaddrinuse ') { // You can specify the processing that is required to execute when the address and port number are occupied }})
5. By default, the client and server each HTTP operation, will establish a connection, the client-server interaction between the completion of the connection is interrupted. In HTTP1.1 , add support for long connections. If the client sends a request header message or the server sends a "connection:keep-alive" message to the response header , the HTTP connection will remain connected . Clients can continue to make requests to the server side through the same connection.
6. In node. js, when the client establishes a connection to the server, the connection event that triggers the HTTP server object can be specified by listening for the event and specifying the action to be taken when the connection is established, in the callback function that is invoked when the event is triggered
Server.on (' connection 'function() { // callback function })
Note: When you access the HTTP server in the browser, the browser will issue two requests from the customer, one at a time, and the other is the request made automatically by the browser for the display icon in the Favorites folder (default is Facicon.ico).
7. You can use the SetTimeout method of the HTTP server to set the server time-out. When the timeout expires, the client cannot continue to take advantage of the connection established with the HTTP server
Server.settimeout (Msecs,callback)
Parameter description: Mecs is an integer value that sets the time-out period for the server in milliseconds. You can cancel the server's time-out processing by setting the parameter value to 0
The callback parameter is used to set the callback function that is called when the server times out, and a parameter is used in the callback function that the server uses to listen to the client request's socket object.
When the server times out, the timeout event that triggers the server object can also be used without using the callback callback function in the SetTimeout method, but by listening to the timeout event
Server.on (' timeout ',function() { // callback })
In addition, the server has a timeout attribute with an integer value, in milliseconds, that can be used to view or modify the server's time-out.
< two > Get client request Information
The first parameter value in the callback function that is called when the HTTP server receives a client request is a Http.incomeingmessage object that is used to read data from the client request stream, so that when new data is read from the client request stream, the data event is triggered, and when it is read through the client request stream Sends an end event.
For example, get/post usage is just a reference, for module URLs and mkquery-string see the url/query-string Common method Description for node. js Get/post
varurl = require (' URL '));varqueryString = require (' query-string '));varHTTP = require (' http '); Http.createserver (function(req, res) {//1.get varobj = Url.parse (Req.url,true); varUrlstr =Obj.pathname; varGET =Obj.query; //2.postLet str = "; Let POST=NULL; Req.on (' Data ',function(data) {str+=data; }); Req.on (' End ',function() {POST=querystring.parse (str); Console.log (POST); });}). Listen (9696);
Basic knowledge of node Basic Learning--http