Summary
What is a Web server?
Web servers generally refer to Web servers, which are programs that reside on some type of computer on the Internet, and the basic function of Web servers is to provide web information browsing services. It only supports HTTP protocols, HTML document formats, and URLs, in conjunction with the client's Web browser.
Most Web servers support the service-side language (PHP, Python, ruby,asp.net), and so on, and retrieve the data from the database through the language, returning the results to the client browser.
At present, the most mainstream three Web servers are Apache, Nginx, IIS.
Create a Web server using node
node. JS provides HTTP modules, which are used primarily to build HTTP server and client. Introducing the HTTP Module
// introducing the HTTP module var http=require ("http");
Create a Web server
Create a Server.js file with the following content:
//introducing the HTTP modulevarHttp=require ("http");//Introducing the FS modulevarFs=require ("FS");//Introducing the URL modulevarUrl=require ("url");//Creating a serverHttp.createserver (function(request,response) {//parse the request, including the file name varPathname=Url.parse (request.url). Pathname; //Enter file nameConsole.log ("Request for" +pathname+ "received."); //read the requested file contents from the file systemFs.readfile (PATHNAME.SUBSTR (1),function(err,data) {if(Err) {Console.log (err); //HTTP status code 404 Not FoundResponse.writehead (404,{"Content-type": "Text/html"}); }Else{Response.writehead (200,{"Content-type": "Text/html"}); //Response File ContentsResponse.Write (Data.tostring ()); }; Response.End (); });}). Listen (5544); Console.log ("Server running at http://127.0.0.1:5544");
Start debugging
The status code for the browser response is 404 because the file was not found on the server when accessing/
Web server
Browser
Next, we create a page called index.html, which reads as follows:
<HTML> <Head> <title>My first page.</title> </Head> <Body> <H1>Hello my page world.</H1> </Body></HTML>
Start server, browse http://127.0.0.1:5544/index.html
Create a Web client from node
Create client.js with the following code:
var http=require ("http" // The option for the request var options={Host: "127.0.0.1" port: ' 5544 ' "/index.html" };
// callback function for handling responses var callback= (response) { var body= " ' data ', function (data) { Body +=data; });
Response.on (' End ', function () {//Data receive completed Console.log (body);});
}; // send a request to the server var req=http.createclient (options,callback); Req.end ();
Output
Information
Http://www.runoob.com/nodejs/nodejs-web-module.html
[Node.js]web Module