About node. js

Source: Internet
Author: User
Tags node server

If it's easier said, node. JS is a server that, like Tomcat, accepts Web requests and, after doing business processing, returns the results to the foreground, node. JS applies to high-concurrency requests, applies to more IO, calculates less business, and explains the features of node. js.


- event-driven What is event-driven? Online a lot of say this many, but all say very confused, give a person tall on the feeling, and touch the mind. In fact, I think the event driver is based on the callback, for example, process A to run to a place where I/O operations, normally process a needs to finish processing I/O before continuing the following operations, under the event-driven model, the operation of the I/O will be processed in parallel with the following program, after I/O processing is completed, The result is handled by the event handler function (that is, the callback function), which reduces the time to wait for I/O.
- main process single thread This is actually the JavaScript language features, so-called "single-threaded" means that only one task can be completed at a time, if there are multiple tasks, you have to queue, the previous task is completed, then perform the next task, and so on. So, in conjunction with node. JS, which is event-based, there is a queue of events in which node. JS executes events in the event queue. What are the benefits of using a single thread? Take a look at what's bad about multithreading first:1, multithreading overhead. If the server is multithreaded (tomcat is so, without a new request, a new thread is opened), then each thread will consume a minimum of 2M of system memory, so if the request is very large, the server will be under a lot of pressure. 2, multithreading prone to errors. Developing multithreaded programs is very difficult, error-prone, programmers need to consider deadlocks, data inconsistencies and other issues.

This avoids the problem of using JavaScript as a single thread, where the main thread is single-threaded, and all the blocked parts are given to a thread pool for processing, and the main thread is collaborating with the thread pool through the queue.


- use the Google V8 engine V8 is a Google-developed open-source JavaScript engine for Googlechrome, which is actually a JavaScript interpreter. V8 is used to interpret JavaScript code in chrome, it can also be used to interpret the code on the server side, which is why node. JS uses JavaScript on the server side. V8 improves performance by compiling JavaScript into machine code, not bytecode, or interpreting it before running.

The above summarizes several features of node. js, which are generally based on events, non-blocking, and low memory consumption, which is suitable for processing high concurrent large data requests. Here's a quick introduction to the use of node. js.


- Installation

first download the Windows version of node. JS, which can be installed directly after the download is complete, with the address:

https://nodejs.org/download/

after the download is complete, the program will automatically configure the path, you can open the Command Prompt window (DOS window), enter "Node", if the error occurs, the installation is successful.
- First small program HelloWorldin the directory where the DOS window is located, create a new Helloworld.js file and enter:
console.log ("Hello World"); Save the file by using the following statement (Node command) to execute:

node Helloworld.js

Normally, the "Hello World" will be output in the DOS window, so we can finish the first small example, and the following will say a little bit more complicated.


- Second applet: processing requests
The goal of this applet: when the user enters the address bar: http://localhost:8888, the page displays "HelloWorld". To create the Server.js file in the current directory, write the following code:

<span style= "FONT-SIZE:14PX;" >   var http = require ("http");   Http.createserver (function (request,response) {        response.writehead (200,{"Content-type": "Text/plain"});        Response.Write ("HelloWorld");        Response.End ();  }). Listen (8888);</span>

This completes a working HTTP server, isn't it simple? Of course we have to let it run and enter in the DOS window:

node Server.js

next open the browser to access http://localhost:8888/, if normal, the page will show "HelloWorld".
End

About node. js

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.