Nodejs Study Note One

Source: Internet
Author: User

A.nodejs Installation

The installation of Nodejs has nothing to say about the default installation. Download the installation package website:Nodejs official website

I am using the window's installation package Node-v4.2.6-x64.msi

Open command line after installation completes view install node version with node-v command, there is a version number showing that the installation was successful.

  

You need to create a folder as a Nodejs server folder, for example, I created a chuanodejs under D:\chua. This folder will be the root path of the node server, which is a separate project.

  

Nodejs Basic use can refer to the Beginner Tutorial Basic tutorial , here does not analyze. This paper is a case study of human Nodejs.

  It is necessary to note that require uses relative paths relative to the current file.

b.npm

NPM is a package management tool that is installed with Nodejs to address many of the issues in Nodejs code deployment, with the following common usage scenarios:

1. Download the third-party package from the NPM server to local use.

2. download and install a third-party command-line program from the NPM server to use locally.

3. Upload the package or command-line program you have written to the NPM server for others to use.

Since NPM has been integrated with the new version of Nodejs, NPM has been installed as well. You can also test for a successful installation by entering "Npm-v" . The following command appears and the version prompt indicates that the installation was successful:

  

NPM installation command:"npm Install" + Module name

  For example, there is a well-nodejs-based frame express framework. Express is a simple and flexible node. JS WEB Application framework that provides a range of powerful features to help you create a variety of Web applications, and rich HTTP tools. Express can be used to quickly build a full-featured website. The site I want to build will be based on this framework, so install first.

First use the command to navigate to my server directory Chuanodejs, and then use the Install command

  

The above command installs the Express framework in the node_modules directory of the current directory, and the Express directory is automatically created in the node_modules directory and saved to the dependency list (-- The Save command can refer to the common commands of ruthless npm ). Here are a few important modules that need to be installed with the Express framework:

    • Body-parser -node. JS Middleware, which handles JSON, Raw, Text, and URL-encoded data.

    • Cookie-parser -This is a tool for parsing cookies. The req.cookies can be used to fetch the transmitted cookies and turn them into objects.

    • multer -node. JS middleware for processing form data that enctype= "Multipart/form-data" (Sets the MIME encoding of the form).

      NPM Install body-parser--savenpm install cookie-parser--savenpm install Multer--save

It should be noted that the installation is divided into NPM Package installation is divided into local installation (locally), the global installation of two , from the command line, the difference is only G, such as

NPM Install Express      //Local installation NPM installed EXPRESS-G   //Global installation

  Local Installation

1. Place the installation package under./node_modules (the directory where the NPM command is running), and if there is no Node_modules directory, the Node_modules directory is generated under the directory where the NPM command is currently executing.

  2. A locally installed package can be introduced via require ().

  The libraries in the project are not installed globally, and NPM installs ./node_modules them in, which you just need to require() use.

  Global Installation

1. Place the installation package under/usr/local.

2. Can be used directly on the command line.

3. A locally installed package cannot be introduced via require ().

Generally installed in the global is a command line requirements of the package, such as Bower, you need to use the command line bower this command.

Test: You can install only one third-party package for multiple projects using it. My current project is Chuanodejs, which installs express under the project. In this directory to create an execution file Server.js, the code is as follows (where index.html is placed in the current directory, you can write a random)

varExpress = Require ("Express");varApp =Express ();//specifies static file path strength static, you can directly access the file under static Test.png:http://localhost:8080/test.pngApp.use (express.static (' Static ')); App.get ("/",function(req,res) {console.log (__dirname); Res.sendfile (__dirname+ "/index.html")})varServer = App.listen (8080,function(){    varHost =server.address (). Address; varPort =server.address (). Port; Console.log ("Application instance, Access address is http://%s:%s", host, Port)})

Execution server

  

Browser access

  

This time I wrote an execution file test.js in the current directory, the source code is as follows

var express = require (' Express '); var app = Express (); App.get (function  (req, res) {   res.send (' Hello World ') ;}) var function () {  var host = server.address (). Address  var port =  Server.address (). Port  Console.log ("application instance, Access address is http://%s:%s", Host, Port)})

Then also execute him, accessing the effect in the browser as

  

Indicates that multiple ports can be monitored under the same project . If you are executing a port (such as Server.js and test.js with port 8080), you will get an error, so avoid

  

So I set up under the Chuanodejs a single file to act as a standalone project, as follows

  

Where Server.js is changed to ( where App.use (express.static ('chuayyqing/static')) The path is relative to the installation root of the Express package. The current Express is installed under Chuanodejs, so the path is relative to the Chuanodejs directory )

varExpress = Require ("Express");varApp =Express ();App.use (express.static (' chuayyqing/static ' ))); App.get ("/",function(req,res) {console.log (__dirname); Res.sendfile (__dirname+ "/index.html")})varServer = App.listen (8080,function(){    varHost =server.address (). Address; varPort =server.address (). Port; Console.log ("Application instance, Access address is http://%s:%s", host, Port)})

Execute code

  

Go to the browser to run.

If you come to a new project next time, you can create a file in the Chuanodejs directory to process it, so you can use the already installed Express package.

referencing modules in code (using the Require reference module) is divided into the following categories

1)http, FS, path, etc., native module, this is installed Nodejs when it has been installed, the code directly used, such as require ("http").

2)./mod or.. /mod, relative path of the file module, in which each module is a JS file, such as server.js in the reference to the same directory Test.js:require ("./test.js").

3)/pathtomodule/mod, Absolute path of the file module.

4) MoD, non-native module of the file module, such as third-party Package Express, the installation is successful after direct use, such as require ("Express").

  

Today's notes first come here, at least the homepage can run up.

  If you feel this article is good, please click on the bottom right "recommended"!

Nodejs Study Note One

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.