Play to node. JS (one)

Source: Internet
Author: User
Tags install node node server

Play to node. JS (one)

  Before we talk about node. js, let's say JS, if you've ever developed a front end, then you must have come into contact with something that's called JavaScript, and for JavaScript, you only have basic operations--adding interactivity to a Web page. But this is far from enough, far from the site you imagined, no complex cool features so you have to learn Java, PHP, Ruby, Python to write back-end programs for the site. During this period, JS extended, with jquery and prototype framework to make it easier for you to write JS, but this is just the front-end AH ~ At this time, node. js Volley was born, like a treasure of a good sword suddenly burst out of dazzling light, known as the server-side JS. From the 90 's in the Middle Ages as a small toy for the enhancement of DHTML, to the front-end technology, like jquery, to the current service-side technology, JS has undergone a lot of changes, but also a lot of progress. The following is the server-side JS introduction.

Server-Side JS 

  JavaScript is first run in a browser, but the browser simply provides a context that defines what can be done with JavaScript, but does not "say" too much about what the JavaScript language itself can do. In fact, JavaScript is a "complete" language: It can be used in different contexts, and its ability is much more than that of other similar languages.

node. js is actually another context that allows JavaScript code to run in the backend (out-of-browser environment).

To implement JavaScript code running in the background, the code needs to be interpreted and executed correctly. This is how node. js works by using Google's V8 virtual machine, the JavaScript execution environment used by Google's Chrome browser, to interpret and execute JavaScript code.

In addition, there are many useful modules along with node. js that can simplify many repetitive chores, such as outputting strings to a terminal.

As a result, node. JS is actually both a runtime environment and a library. So the question is, what can node. js do?

What can node. js do 

Nodejs's author says that his goal of creating nodejs is to achieve a high-performance Web server, and he first values the superiority of the event mechanism and asynchronous IO model, not JS. But he needs to choose a programming language to implement his idea that the programming language does not have IO capabilities and needs to be able to support the event mechanism well. JS does not have the IO feature, is inherently used to handle DOM events in the browser, and has a large group of programmers, so it becomes a natural choice.

As he wishes, Nodejs is active on the service side, and a large number of Nodejs-based Web services appear. On the other hand, Nodejs let the front end as the artifact, and finally let their ability to cover the range out of the browser window, a large number of front-end tools such as springing up.

Therefore, for the front end, although not everyone has to take Nodejs to write a server program, but simple to use the command interactive mode debugging JS code snippets, complex to write tools to improve productivity.

To use node. js, you first need to install it. For more information on how to install node. js, you can refer directly to the official installation guide. After the installation is complete, continue back to read the following content.

First Program: Hello World

Did you finish the installation? If it is OK, you can take a look at the terminal input:

1 $ node--version2 v0.10.35

Review the version number and enter "node" to interact with the following interface:

1 $ node 2 >3 2

You can now enter. Help to see what commands are available (PS: Don't forget to have ".") ):

 1  > help  2 .  break  Sometimes you get stuck, this   Get It out  Span style= "color: #008080;" >3 . Clear Alias for . break  4  .exit exit the REPL  5  6  .load load JS from a file to the REPL session  7 . Save Save All evaluated Commands in  

So now use. Exit to quit, node.

OK, "nonsense" not much to say, immediately began our first node. js application: "Hello World".

Open your favorite editor and create a hello.js file. What we're going to do is to output "Hello world" to stdout, and here's the code to implement the feature:

1 console.log ("Hello World");

Save the file and execute it through node. JS:

1 $ node Hello.js 2 Hello world!

Normally, it will output Hello World at the terminal.

Well, I admit that the application is a little boring, so let's get some "dry".

Hello World Server Edition

Create a new file with your favorite editor, Named Server.js or other name you like, the pest you can see "Hello World" on the page of the page, the following is the code to implement the function:

1 var http = require ("http"); 2 3 http.createserver (function(request, Response) {4   response.writehead (200 , {"Content-type": "Text/plain"}); 5   Response.Write ("Hello World"); 6   Response.End (); 7 }). Listen (8888); 8 console.log (' Server is running at http://localhost:8888 ')

The code is explained in the following first:

    • The first line means to introduce the ' HTTP ' module and give it an ' HTTP ' variable for use.
    • The third line to line sixth is to use the HTTP module to create a server-side service, The function receives req and res two objects, which are requested request and response, respectively. The request contains various information from the browser, such as query, body, headers, etc., can be accessed through the Req object. Res object, we generally do not take information from the inside, but through it to customize the information we output to the browser, such as header information, such as the content you want to output to the browser. Here we call its Send method, outputting a string to the browser.
    • Line seventh is defined so that it listens on port 8888.
    • The last line executes after the listen action succeeds, and we perform a command-line output that tells us that the listener action is complete.

Save the file and execute it through node. JS:

1 $ node Server.js 2 Server is running at http://localhost:8888

  Normally, open the browser and enter "http://localhost:8888/" in the address bar to see "Hello World".

Well, this is the main introduction of the so much ~ Welcome to shoot Bricks

PS: This blog Welcome to forward, but please specify the blog address and author, because I level limited, if there is wrong, welcome point, Thank you ~

Blog Address: http://www.cnblogs.com/voidy/

Blog: http://voidy.net

<. ))) ≦

Play to node. JS (one)

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.