node. JS Foundation--01 Hello,world

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.