node. JS Road "First" knowledge of node. js

Source: Internet
Author: User
Tags install node nginx reverse proxy

What is node. js

1. Node. JS is the Javascrip that runs on the server.

2. Node. JS is a platform based on a chrome Javascrip run-time resume.

3. Node. JS is a non-blocking I/O server-side JavaScript environment, based on Google's V8 engine, the V8 engine executes JavaScript very quickly.

See the introduction of Xia Guan Network:

 is  for easily building fast, scalable network applications. node. JS uses an event-driven, non-blocking i/and for data-intensive real-time Applications that run across distributed devices.
node. JS Usage Scenarios

See the pros and cons of node JS before you look at his usage scenarios.

First, Node JS features

1. He is a JavaScript runtime environment

2, rely on the chrome V8 engine for code parsing

3. Event-driven non-blocking I/O

4, lightweight, scalable, suitable for real-time data interactive applications

5, single process, single thread

What is event-driven? What non-blocking I/O can refer to my article, which describes the asynchronous IO model in detail, can be used to understand the benefits of Node JS's event-driven and non-blocking I/O.

Http://www.cnblogs.com/luotianshuai/p/5098408.html

Second, Node JS Excellent, shortcomings

Advantages:

1, concurrency High is the choice node JS important advantages

2. Suitable for I/O intensive applications

Disadvantages :

1, not suitable for CPU-intensive applications, CPU-intensive applications to node challenges mainly: Because of JavaScript single-threaded reason, if there are long-running computations (such as cycle), will cause the CPU time slices can not be released, so that subsequent I/O can not be initiated;

Solution: The decomposition of large operational tasks for a number of small tasks, so that the operation can be released in a timely manner, does not block the initiation of I/O calls.

2, because it is single process single-threaded only use one CPU, can not fully use the CPU to cause the waste of resources

3, low reliability, a piece of code crash, the whole system will crash

Reason: He is single-threaded, but single-process

Workaround:

3.1, Nginx reverse proxy load balancing open multiple processes, bind multiple ports

3.2, open more than one process to listen to the same port, using the cluster module

4, open Source component library quality is uneven, update fast, downward incompatible

5, Debug inconvenient, error does not stack trace

Third, suitable for node JS scene

1. Restful API

This is Nodejs ideal application scenario, can handle tens of thousands of connections, itself does not have too much logic, only need to request the API, organize the data to return. It essentially simply looks up some values from a database and makes them a response. Because the response is small text, inbound requests are also small amounts of text, so traffic is not high, and a machine can even handle the API needs of the busiest companies.

2. Unify the UI layer of the Web application

The current MVC architecture, in a sense, Web development has two UI layers, one in the browser we finally see, the other on the server side, responsible for generating and splicing pages.

Not discussing whether this architecture is good or bad, but there is another practice, service-oriented architecture, better to do the front-end dependency separation. If all of the key business logic is encapsulated as a rest call, it means that you need to think about how to build a specific application with these rest interfaces on top. Those back-end programmers don't worry about how specific data is delivered from one page to another, and they don't have to worry about whether user data updates are asynchronously acquired through Ajax or by refreshing the page.

3, a large number of Ajax requests

For example, the personalization application, each user sees the page is different, the cache is invalid, needs to launch the AJAX request when the page loads, or the barrage system many users simultaneously through the comment, the NODEJS can respond the massive concurrent request. In summary, Nodejs is suitable for scenarios where high concurrency, I/o intensive, and small business logic are used.

Note: Node js is very good it can implement a lot of applications, when we want to use it to complete an application, the need to consider is appropriate to do it.

Node JS Installation Configuration

1, download the source code more version: https://nodejs.org/en/download/

cd/work/app/wget http://nodejs.org/dist/v0.10.24/node-v0.10.24.tar.gz

2. Extracting source code

TAR-ZXVF node-v0.10.24.tar.gz

3. Compile and install

CD node-v0.10.24. /configure--prefix=/work/appmakemake Install

4, configure Node_home, enter the profile edit environment variable

Vim/etc/profile

Set the NODEJS environment variable to add the following on the export PATH USER LOGNAME MAIL HOSTNAME histsize histcontrol line

Export node_home=/work/app/node/0.10.24export PATH= $NODE _home/bin: $PATH

: Wq Save and exit, compile/etc/profile make configuration effective

Source/etc/profile

Verify that the installation configuration is successful

Node-v

Output v0.10.24 indicates a successful configuration

NPM Module Installation Path

/work/app/node/0.10.24/lib/node_modules
MAC Install node JS

Node JS is installed on the server to quickly develop node JS so install node JS on Mac and use Pycharm to develop

First, install node JS

https://nodejs.org/en/download/

Download and install node JS it will install node. js and NPM (Node Package Manager) on your machine.

The first application of Node JS

If we use PHP to write back-end code, we need an 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. Kinda like my big Django and tornado

Before we create the first "Hello, world!" app for node. JS, let's start by understanding which parts of the node. JS application are composed:
Required Module : We can use the Require directive to load the node. JS module.
Create server : The server can listen to the client's request, similar to Apache, Nginx and other HTTP server.
receive requests and response requests : The server is easy to create, the client can send HTTP requests using a browser or terminal, and the server receives the request and returns the response data.

1, Import node JS module "and Python in the import of a concept"

node. JS Road "First" knowledge of node. js

Related Article

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.