Discover node. JS (a)--node.js Introduction and installation Configuration

Source: Internet
Author: User

1.node.js Introduction and Features:

node.js is a JavaScript The off-browser binding runs in a normal server environment, just like running python php Ruby the same procedure. We can use node. js easy server-side application development, Python , perl Php Ruby > Almost everything can be done, and can do better.

node.js I/O node. js using a single-threaded model for all I/o All adopt the asynchronous request way, avoids the frequent context switch. node. js during execution, an event queue is maintained, and the program enters the event loop at execution time waiting for the next event to arrive, each asynchronous I/O When the request is complete, it is pushed to the event queue, waiting for the process to be processed.

The advantage of doing this is that CPU and memory focus on one thing at a time, while simultaneously executing the time-consuming I/O operations as much as possible . For low-speed connection attacks,node. js simply increases the request in the event queue, waits for the operating system to respond, so there is no multithreading overhead, which can greatly improve the robustness of the WEB application and prevent malicious attacks. Of course, the drawbacks of the async pattern are also obvious, because it does not conform to the developer's conventional linear approach, and often involves splitting a complete logic into events, adding to the difficulty of development and debugging (node. js 's third-party module presents a lot of solutions).

node. JS replaces multi-threading with asynchronous I/O and event-driven, resulting in considerable performance gains. In addition to using V8 as the JavaScript Engine, node. JS also uses efficient Libev and Libeio libraries to support event-driven and asynchronous I/O.

2.node.js Installation and configuration:

node.js can be installed following POSIX " Mac OS x solaris Windows Windows Azure announce to node. js fully supported, but node. js and Windows compatibility is still not as good as POSIX operating system, in the npm The third-party modules provided are particularly prominent. It is not recommended to do node. js Windows bit ' CentOS 6.6 install node. js :

[Email protected] ~]# cd/usr/local/src/

[Email protected] src]# wget http://nodejs.org/dist/v0.12.2/node-v0.12.2-linux-x86.tar.gz

[Email protected] src]# TAR-ZXF node-v0.12.2-linux-x86.tar.gz

Since we are downloading a free-build installation package, we can use it after decompression. To move the extracted directory to any directory, configure the environment variables:

[Email protected] src]# MV Node-v0.12.2-linux-x86/opt/node

[Email protected] src]# Vim/etc/profile

Exportpath= $PATH:/opt/node/bin

[Email protected]]# Source!$

at this point, Node . JS is already installed. Test whether node. JS is already in effect:

[Email protected] ~]# vim Test.js

function Hello () {

Console.log ("Hello World");

}

Hello ();

[Email protected] ~]# node Test.js

Hello World

the above results show that Node . JS is ready to use.

Node . JS also has a number of third-party modules that can be greatly facilitated by these third-party packages (such as the Express module, which is a very stable and widely used Web development Framework). Use the NPM command to install a third-party module,NPM- like apt-get under Ubuntu ,yum under CentOS :

[[email protected] ~]# NPM Install-g Express

since npm obtains and downloads package information by default from a foreign source, it is generally very slow to download and can use its --registry parameter to customize the source:

[Email protected] ~]# NPM Install-g Express--registry=http://registry.npm.taobao.org/

Other versions of the installation package can be the node. JS website is downloaded with the following address:

https://nodejs.org/download/

3.node.js Preliminary Understanding:

we have just introduced a There are two ways to do this, in addition to Hello World:

(1)-e parameter: eval () similar to JavaScript :

[Email protected] ~]# node-e ' Console.log ("Hello World"); '

Hello World

(2) the REPL(read-eval-printloop) loop that enters node:

[Email protected] ~]# node

>console.log ("HelloWorld");

Hello World

Undefined

the undefined here is the return value of Console.log. If you enter an incorrect instruction, REPL will immediately display an error and output the call stack. Press CTRL + C two times or press CTRL + D once to exit node. js REPL mode. This mode is very convenient for application development, for example, we can test whether a package is working properly, call one of the application's modules separately, perform a simple calculation, etc.

if it is only HelloWorld seems too low , now we're trying to build an HTTP server using node. js. node . js strips The HTTP server for direct browser users.

[Email protected]~]# vim app.js

App.js

var http =require (' http ');

Http.createserver (function (req,res) {

Res.writehead (200,{' content-type ': ' text/html ');

Res.write ('

Res.end (' <p>HelloWorld</p> ');

}). Listen (3000);

Console.log ("Httpserver is listening at Port 3000.");

[Email protected]~]# node App.js

after that, you can access the server with the port number of http://yourhostip:3000. This program invokes the HTTP module provided by node. js, replies to the same content for all HTTP requests, and listens on the port.

here, let's start with Node . js to do a preliminary understanding, more content after slowly spread, the sauce first ~

Resources:

the node. JS Development Guide byvoid


This article is from the "barrel of fake dog excrement" blog, please be sure to keep this source http://xitongjiagoushi.blog.51cto.com/9975742/1633640

Discover node. JS (a)--node.js Introduction and installation Configuration

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.