1 varHttp=require ("http");//introducing the HTTP module2Http.createserver (function(Request,response) {//Creating a server3Response.writehead ("$", {"Content-type": "Text/html;charset=utf-8"});//Start4 if(request.url!== "/favicon.ico") {//manually clear the browser's default second request so that the code inside will only execute once; otherwise it will execute two times. It has been automatically used in other frameworks such as Express. 5Console.log ("AAA");6Response.Write ("Hello World");7Response.End (' Hello, world ');//If you don't write, you can write an empty string, but it must be a string format.8 }9}). Listen (8000);TenConsole.log ("Server running at http:127.0.0.1:8000");
This section is the Foundation for node. JS, and the Code section shows the use of node. js to create a simple server feature.
The following points to note:
1,var http=require ("http"); Which module needs to be introduced in advance; (This is similar to the introduction of the function in section II);
2,response.writehead (); This method is the header of the file that defines the HTML; must, necessary;
3,if (request.url!== "/favicon.ico") {//manually clear the browser's default second request so that the code inside will only be executed once, otherwise the background output will be executed two times. It has been automatically used in other frameworks such as Express.
4,response.end (' Hello, world '); If you don't write, you can write an empty string, but it must be a string format.
Over ...
node. JS Foundation--01 Hello,world