nodejs foundation to build a simple web server responds with a plain text block http

First talk about, we usually visit on the browser page, see content, in fact, is passed over a web server, such as our visit www.baidu.com. When we entered after the browser address bar, the browser will send request to the web server and a web server according to the information carried in the request, returns content.

 

So, nodejs the http module, is used to set up a web server used.

Let's build a simple server:

var HTTP = the require ( "HTTP" ); 

// Request: a parameter request, which carries information request brought. response: the response parameter will be returned to the information carriers of the browser, these two parameters are examples stream 
var Server http.createServer = ( function (Request, Response) { 
    the console.log ( "Request Received" );
     / / response of header information write writeHead response, the status code is the first parameter, the second parameter is an object, which can write information back to the browser, such as Content-Type: Specifies the return browsing What's content, such as images, html, json, pdf, plain text, etc. 
    // browser based on header information returned by the server, to think of how to render 
    response.writeHead (200, { "content- Type": "text / plain "}); // text / plain: representatives returned plain text 
    Response.Write (" the Hello OUT from the Application "); // will write the contents written in the parameter of years, back to the browser 
    response.end ( );
}) 

// let the server listens on a port 
server.listen (3000 ); 

console.log ( "Server Started ON localhost Port 3000"); // add a server to start up tips

Then perform node app will be up and running in a terminal server

Then, in the browser enter localhost: 3000 you find on the page will appear, we write the contents of the response method parameters in! ! !

 

 

 

listen can pass the second parameter, ip

Returned to the browser's content can also be written in the end the method's response:

var HTTP = the require ( "HTTP" ); 

// Request: a parameter request, which carries information request brought. response: the response parameter will be returned to the information carriers of the browser, these two parameters are examples stream 
var Server http.createServer = ( function (Request, Response) { 
    the console.log ( "Request Received" );
     / / response of header information write writeHead response, the status code is the first parameter, the second parameter is an object, which can write information back to the browser, such as Content-Type: Specifies the return browsing What's content, such as images, html, json, pdf, plain text, etc. 
    // browser based on header information returned by the server, to think of how to render 
    response.writeHead (200, { "content- Type": "text / plain "}); // text / plain: representatives returned plain text 
    // Response.Write (" the Hello OUT from the Application "); // write the contents written in the parameter of years, back to the browser 
    response. End ( "Hello from OUT file application") ;End add a method to indicate the end of the response 
}) 

// let the server listens on a port 
server.listen (3000, "127.0.0.1" ); // You can also add a second parameter 127.0.0.1 represents the local 

console. log ( "server started ON localhost Port 3000"); // add a server to start up tips

 

There will function in http.createServer it out, write:

var HTTP = the require ( "HTTP" ); 

// Request: a parameter request, which carries information request brought. response: the response parameter will be returned to the information carriers of the browser, these two parameters are examples stream 
var  onRequest = function (Request, Response) { 
    the console.log ( "Request Received" );
     // Response of writeHead was written in response to the header information, the first parameter is the status code, the second parameter is an object, which can write information back to the browser, such as Content-Type: specifies what is returned to the browser content, such as images, html, json, pdf, plain text, etc. 
    // browser based on header information returned by the server, to think of how to render 
    response.writeHead (200, { "content- Type": "text / plain"} ); // text / plain: representatives returned plain text 
    // ( "the Hello OUT from the Application") Response.Write; // write the contents written in the parameter of years, back to the browser 
    response.end ( "hello from out application ");
}

 Var Server = http.createServer ( onRequest ); 

// let the server listens on a port 
server.listen (3000, "127.0.0.1"); // can also add a second parameter representative of 127.0.0.1 is a local 

console. log ( "server started ON localhost Port 3000"); // add a server to start up tips

 

Stuck in which

 

Guess you like

Origin www.cnblogs.com/fqh123/p/11256631.html