One of node. JS Development Notes • Introductory article

Source: Internet
Author: User
Tags install node npm install node node inspector


Preface:

The name node. JS is no stranger. At first, I thought it was another group of JS class library. As a faithful follower of jquery, he was still more concerned about the emergence of the heresy. Later found that, in fact, the server-side JS. With the use of the Java EE so many years, no mind to engage in this set. It's better to write down the architecture of the Java EE or something. The storm of technological innovation has swept through the IT community. I once again opened the learning talent.

Environment Construction:

software Download:

1. Node. JS installation Package

(See Official blog: http://blog.nodejs.org update):

Http://nodejs.org/dist/v0.11.15/node-v0.11.15-x86.msi (32-bit)

Http://nodejs.org/dist/v0.11.15/x64/node-v0.11.15-x64.msi (64-bit)

Software Installation:

1. All-way carriage return installation

Remember your installation path. My path is D:\Program Files\nodejs ( for The path of the installation of different words, the next involves the path to self-repair )

After installation, as with most open source products, you have to configure the environment variables.

2. Setting Environment variables

To see "System variables", there should be "D:\PROGRAMFILES\NODEJS;".

See "User Variables" with "C:\USERS\PC\APPDATA\ROAMING\NPM".

If not, add it yourself.

3. Run node. js

Open "All Programs" and open "node. js command Prompt".


WIN8 environment "win+ R" opens the command to run the window. The same is true if you post the command below.

c:\windows\system32\cmd.exe/k "D:\Program Files\nodejs\nodevars.bat"


It's best to write a bat file to put on the table before trouble. You should be able to see a black screen as follows when you open it:

He will tell you that your node. js and NPM (node package Manager) tools are already installed. Then NPM is the protagonist. This is equivalent to a role like Maven in the Java World, which is package management. Students who have worked with MAVEN will feel very friendly. The disadvantage is to rely on the network. The next section will talk about this piece.

When the environment is ready, write a Hello world to celebrate it.

var http =require ("http"); Http.createserver (function (request,response) {       Response.writehead (              Content-type ":" Text/plain "       });       Response.Write ("Hello World");       Response.End ();}). Listen (console.log); (' serverrunning at http://127.0.0.1:3000 ');


Console display:

You can see Hello World with a visit.


4. NPM uses

Npm,node Package Manager. As the name implies is to help us download the class library. Check the respective network configuration before you begin. I spent a long time in the intranet environment. For projects with network dependencies The first thought is to set up an agent. There is the gfw of the pit father. The following two sentences are well-equipped with NPM's agent.

NPM config Set proxy=http://127.0.0.1:8087npm config set registry=http://registry.npmjs.org

Next, install a more important development-related package-supervisor.

Install command: NPM install Supervisor-g

The above message appears without any other error ("304" status code is because I have already installed). Supervisor even if it's installed. Npminstall [ package name ] This command is the most commonly used command in NPM. The default is to install the current path, plus the- g parameter indicates the installation to the global.

Install to the current path and the package will be downloaded to the Node_modules folder.

Installing to the global will put the common path, like my humble: C:\Users\pc\AppData\Roaming\npm\node_modules

The global package is available to all projects and is generally not modified. If there is a need to modify a package, do not put it on the global domain. There is also a plug-in feature. If you do not want to download through the network, you can directly copy to the Node_modules directory is OK.

Words at two ends, package management This piece first speak so much. Why is it so important to look back at supervisor? Then looking down, supervisor is quite a hot deployment plugin in node. js. If you do not install this plugin, you will need to restart the project every time you modify it. Supervisor is actually a monitoring program, and whenever you change the file, he helps to restart automatically.

Or with the hello_world.js we just wrote. Encapsulate the welcome speech in a function.

var http = require ("http"); Http.createserver (function (request, response) {       response.writehead (200,{              ") Content-type ":" Text/plain "       });       Response.Write (DoSomething ());       Response.End ();}). Listen (console.log); (' Server running at http://127.0.0.1:3000 '); function dosomething () {       


Then execute the Supervisor Hello_world.js. As shown, the console tells me that he is already monitoring my project.

Then I changed the welcome speech.

function dosomething () {       return ' hi! Jack! ';}


Then look at the console and the browser. It sure did help us restart. This is the program industry comrade Lei Feng!


5. Debugging Tools

工欲善其事 its prerequisite. The most powerful skill a programmer has is to catch a bug. The Debug tool has become a second concern after the thermal deployment. Many tutorials on the web, I prefer to node-inspector debugging tools. JavaScript is originally a family, how can you bear to use two debugging methods? Chrome V8 provides a platform for us.

Install command: NPM install Node-inspector-g

Download something a bit more ready for the speed of the network. This package is about 14.2 MB. Don't dwell on it.

Install and then debug our hello_world.js. Execute node--debug hello_world.js

Then open a separate command-line window to execute the Node-inspector

Will prompt us to visit: http://127.0.0.1:8080/debug?port=5858

Open http://127.0.0.1:8080/debug?port=5858 with Chrome

Will find a familiar interface. Appearance and function front end JS debugging basically consistent.

Probably because the version used is too high the debugger is only partially functional compatible.

The current version of Node-inspector is: Node Inspector v0.8.3

6. Official recommendation framework

awaited, node. JS only recommends the official Framework Express. This frame one makes us easy farewell to the Hello World era, one second to become tall on.

Install command: NPM install Express-g

NPM Install Express-generator-g

Once installed, enter workspace to create a new project named Node.express.

Executive: Express node.express

Generate Express Project basic files

Go to Node.express Path, execute: NPM Install

Resolves package dependencies. The Express project is basically good. Do you think happiness is too sudden to be ready? I have to say that the official recommendation of the framework is still in the mind to do.


Execution: NPM start

Start the project.

View the Package.json file under the directory. You can see the entrance of Express to the./bin/www file. NPM start is the same as node/bin/www.

{  "name": "Node.express",  "version": "0.0.0",  "private": True,  "scripts": {    "start": "Node./bin /www "  },  " dependencies ": {    " Body-parser ":" ~1.10.1 ",    " Cookie-parser ":" ~1.3.3 ",    " Debug ":" ~ 2.1.1 ",    " Express ":" ~4.11.0 ",    " Jade ":" ~1.9.0 ",    " Morgan ":" ~1.5.1 ",    " Serve-favicon ":" ~2.2.0 "  }}


Knowing this can facilitate our thermal deployment. Execution: Supervisor./bin/www

Summarize:

Thank you very much for seeing the children's shoes here. The above content basically covers the Getting started content of node. js. node. js as a new language. Its unique asynchronous mechanism optimizes the loading speed and interface services of our pages. Predict the funeral, and listen to tell.

One of node. JS Development Notes • Introductory 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.