node. js module and NPM

Source: Internet
Author: User

1. Module concept

Native module: The native module provided by the node. JS API, the native module is loaded at startup.

File module: Dynamic loading module, which is implemented and completed by the native modules module.

The file module needs to be loaded by calling the Require method.

node. JS has a cache of both modules and does not repeat the overhead to load the module, reading only the corresponding data.

Invocation of native modules

  The node. js API require loads the appropriate node. JS module and returns a node. JS Module object after the load succeeds.

The object owns all the methods and properties of the module.

var HttpModule = require ("http");

Httpmodule.createserver (function (res,req)). Listen ();

The invocation of the file module

The path must be specified when loading

absolute path:var test = require ('/path/.../test.js ');

relative path:var test = require ('./test.js ');

In a file module, only exports and Module.exports objects are exposed to external properties and methods to be invoked by returning require objects

  

NPM Package Manager (need to maintain network)

To download the required modules via NPM, thenpm Install command will be added in this directory Node_ Modules folder to store downloaded modules

Download NPM Install module

Uninstalling NPM Uninstall model

  NPM Install-g Express

strong> strong> npm install Express

  

  

Express App

Node App.js

strong> strong> npm install Socket.io

strong> strong> NPM install MySQL

Node. js module and NPM

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.