"Node. JS Learning One" first node. js Application--hello world!

Source: Internet
Author: User
Tags node server

When we use PHP to write background code, we need to use Apache or Nginx HTTP server, with MOD_PHP5 modules and php-cgi.

From this perspective, the entire "Receive HTTP request and provide Web page" requirement does not need PHP to handle at all.

But for node. JS, the concept is completely different. When using node. js, we are not only implementing an application, but also implementing an entire HTTP server. In fact, our web application and the corresponding Web server are basically the same.

To create a "Hello World" app money, let's first look at what parts of node. JS applications are made up of:

1. Introduction of the required module: We can use the require directive to load the node. JS Module

 2. Create a server: the server can listen for client requests, similar to HTTP servers such as Apache,nginx

  3. Accept requests and response requests: The server is easy to create, the client can use the browser or terminal to send HTTP requests, the server accepts the request and returns the response data.

Creating a node. JS application introducing the required module

We use the Require instruction to load the HTTP module and assign the instantiated HTTP value to the variable http

var http = require ("http");
Creating a server

Next we use the Http.createserver () method to create the server and bind port 8888 using the Listen method, which receives and responds to data through the request and response parameters.

Examples are as follows:

  

//introducing the required modulevarHTTP = require (' http '); Http.createserver (function(Request, response) {//Send HTTP Header    //HTTP status value 200:ok    //content Type: Text/plainResponse.writehead, {' Content-type ': ' Text/plain ' } ); //Send response DataResponse.End (' Hello world!\n ');
//Setting the Listening port}). Listen (' 8888 ');
//The terminal prints the following informationConsole.log ("Server running at http://localhost:8888");

The above code we have completed a working HTTP server, the code is saved in the node. JS installation root directory, under the root directory using the node command to execute the above code

G:\nodejs>node Server.jsserver running at http://locahost:8888

Next open the browser to access http://localhost:8888, you will see a page written with HelloWorld.

Issues that occur during the writing process:

This part is very simple, but in the process of writing the code, the difference between the size brackets is neglected, in the Writehead function, the content type is enclosed by braces rather than parentheses.

"Node. JS Learning One" first node. js Application--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.