Node JS Learning (i)

Source: Internet
Author: User
Tags node server

1. Introduction

JavaScript is a script run in the browser, it is simple, lightweight, easy to edit, this script is usually used for front-end programming of the browser, but a developer Ryan discovered that the front-end scripting language can run on the server, a storm sweeping the world began.

node. JS is a platform built on the chrome JavaScript runtime to easily build responsive, easy-to-scale Web applications. node. js using event-driven, non-blocking I/O modelsLightweight and efficient, it's ideal for data-intensive, real-time applications running on distributed devices. Node is a JavaScript run environment (runtime). It actually encapsulates the Google V8 engine. The V8 engine executes JavaScript very fast and performs very well. Node optimizes some of the special use cases, providing an alternative API that allows V8 to run better in a non-browser environment. 2. Node. JS Benefits
    • The asynchronous and event-driven APIs of the node. JS Library are all asynchronous and non-blocking. It mainly refers to node. JS-based servers that do not wait for the API to return data. After the server moves to the next API call, node. js occurs after the event notification mechanism helps the server get a response from the previous API call.

    • Very fast built-in Google Chrome's V8 JavaScript engine, the node. JS Library code execution is very fast.

    • Single-threaded but highly extensible-node. JS uses a single-threaded model with circular events. The event mechanism helps the server respond in a non-blocking manner and makes the server highly extensible, rather than creating a thread limit to handle the requested legacy server. node. JS uses a single-threaded program, but can provide a much larger number of requests than traditional servers such as the Apache HTTP server.

    • No buffering-the app for node. JS never uses buffering any data. These applications simply output data in blocks.

    • License Agreement-node. JS is published under the MIT agreement

3, download installation (Window 64-bit version) 64-bit: Http://pan.baidu.com/s/1geaR3OZ installation steps: Directly double-click the Node-v0.10.26-x86.msi file by default installation. Configure environment variables: By default, the path of the node that we just installed will be added to path in the post installation path, and there is no need to manually add it to the PATH environment variable. To see if the installation was successful: in CMD go to the path you just installed, typing the following command to query its version

If the following interface appears, the installation configuration is successful.

4. Nodejs Application Components
    1. introducing the required module: We can use the require directive to load the node. JS module.

    2. Create server: The server can listen to the client's request, similar to Apache, Nginx and other HTTP server.

    3. receiving requests and responding to the request server is easy to create, and the client can send HTTP requests using a browser or terminal, and the server returns the response data after receiving the request.

5. Hello World Procedure Step one, introduce required module

We use the require directive to load the HTTP module and assign the instantiated HTTP value to the variable HTTP, as shown in the following example:

var http = require ("http");
Step two, create the server

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

As an example, create a file called Server.js in the root directory of your project and write the following code:

 var  http = require (' http '  function   //  send HTTP header  //  HTTP Status value: 200:ok  //  content type: Text/plain  response.writehead ($, {' Content-type ': ' Text/plain '  //  send response data "Hello World"  Response.End (' Hello world\n '  8888 //  terminal prints the following information  Console.log (' Server running at http://127.0.0.1:8888/'); 

The above code we have completed a working HTTP server.

Use the node command to execute the above code:

Node Server.js

The command line outputs the following information, indicating that the server application has been started (a word printed above)

Server running at http://127.0.0.1:8888/

Visit http://127.0.0.1:8888/in the browser and you'll see the "Hello World" page

Parsing the HTTP Server for node. JS:

    • The first line requests (require) node. js's own HTTP module and assigns it to the HTTP variable.
    • Next we invoke the function provided by the HTTP module: Createserver. This function returns an object that has a method called Listen, which has a numeric parameter that specifies the port number that the HTTP server listens on.
6. Node. JS applicable scenario
    • I/O bound application

    • Data flow Applications

    • Data-intensive real-time applications (DIRT)

    • Applications for the JSON API

    • Single page Application

7, not very suitable for use of node. JS Scenarios

The use of node. JS is for CPU-intensive applications.

Thanks: Thank you for reading!

Node JS Learning (i)

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.