Reproduced node. js notes (i) Nodejs, NPM, express installation

Source: Internet
Author: User
Tags using git

Thank the original http://blog.csdn.net/haidaochen/article/details/7257655

node. JS installation under Windows platform

Go directly to Nodejs's official website http://nodejs.org/Download Nodejs installation program, double-click on the installation can be

To test whether the installation was successful:

At the command line input node–v should be able to view the Nodejs version number of the current installation

A simple example
Write a short code, save it as helloworld.js, and see how Nodejs is used.

As follows: The code is primarily to create an HTTP server.

[JavaScript]View Plaincopy
    1. var http = require ("http");
    2. Http.createserver (function (request, response) {
    3. Response.writehead ($, {"Content-type": "text/html"});
    4. Response.Write ("Hello world!");
    5. Response.End ();
    6. }). Listen (8080);
    7. Console.log ("Server running at http://localhost:8080/");

Open the command line and go to the path where the current file is stored and run the Node helloworld.js command

If everything works, you can see the command line output: Server running at http://localhost:8080/

At the same time, in the browser input http://localhost:8080/, you can see a page written HelloWorld.



Installing NPM

NPM has a lot of good nodejs packages to solve common problems, such as using Node-mysql, you can easily link to MySQL through the Nodejs, database operation

In the development process will often need to use other packages, using NPM can download these packages for the program to call

A) If the system does not have Git installed, you can download the files required by NPM directly to HTTPS://GITHUB.COM/ISAACS/NPM.

b) If you have git, you can download it using Git.

[Plain]View Plaincopy
    1. git clone--recursive git://github.com/isaacs/npm.git

After downloading to the NPM file, the command line first goes to the NPM location and enters the following code to install it.

[Plain]View Plaincopy
    1. Node Cli.js install NPM-GF

Install Express

Express is a common framework for NODEJS.

A) Global installation

[Plain]View Plaincopy
    1. NPM Install EXPRESS-GD

b) installed under the current folder

[Plain]View Plaincopy
    1. NPM Install Express

After the installation is successful, the command line prompts NPM info OK

-G represents the installation to the Lib in Node_path, and the-D represents the installation of the dependency kit. If you do not have-G, the current directory will be installed (a Node_modules folder will be created).

Referencing Express packages in projects

Cases:

[JavaScript]View Plaincopy
    1. var express = require (' express ');
    2. var app = Module.exports = Express.createserver ();

If you do not have Express installed, you first need to install an express in the current project folder

After the command line goes to the current path, run

[Plain]View Plaincopy
    1. NPM Install Express

Once the installation is complete, you can see a "node_modules" folder in the current directory with an "Express" folder

Note: Packages referenced in the project will be installed in the "Node_modules" folder

Create a project with Express

Enter "Express project name" on the command line to create a new project under the current folder

The following files are included:

With this method, just create an empty project framework, and a simple instance program, run App.js can view (also need to install the Jade package in the project file directory, method similar to install Express)

Reference website:

node. JS Foundation Http://www.infoq.com/cn/master-nodejs

node. js Getting Started Tutorial http://nodebeginner.org/index-zh-cn.html

node. JS Chinese Document Http://cnodejs.org/cman/index.html

Express Document Http://expressjs.com/guide.html#routing

Cnode Community http://club.cnodejs.org/

Common operations with Command line:

1) After the file is run, bring up the command input field again with CTRL + C
2) to go to a disk under F:
3) go to a folder under CD F:\nodeExamples
4) back to the parent directory CD:

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.