Node.js combat 2: Getting Started module.

NodeJS has extensive tripartite module, with these modules, you can quickly develop a variety of applications. This uses Nodejs can be very convenient and fast development.

1, install and load the kernel module, the tripartite

Use npm can search, install, uninstall module.

Example:

Search Module

By: npm search module name

Node.js combat 2: Getting Started Module

In general, the search will be a lot of content, you can use regular expressions to match in order to find they are most needed, Search, such as:

npm search /^express$/

Node.js combat 2: Getting Started Module

Install the module:

npm install the module name

Node.js combat 2: Getting Started Module

After installation, you can use require statements to load:

Node.js combat 2: Getting Started Module

2, create our own modules

We can use the Node, three-way module, of course, can create our own modules.

Embodiment, a simple write module, as follows:

Node.js combat 2: Getting Started Module

This module provides two methods: method1 hello1 return character, method2 hello2 return character. Save as module.js

And then call it in another file, the calling code:

Node.js combat 2: Getting Started Module

"./" refers to a module loaded in the local path.

The above loading omitted .js, may be written as: var module = require ( "./ module.js");

Look at the implementation:

Node.js combat 2: Getting Started Module

3, uninstall module

Will be cached, any time you can easily use the module loaded.

But sometimes, for some modules, if you do not want to continue to use. You can be uninstalled.

Or in an external module update, you need to get the module content updates? Well, here we need to uninstall module to reload.

Unloading module code is as follows:

Node.js combat 2: Getting Started Module

resolve can get the full path of the module. When the uninstall from the cache, you need to use the full path.

 

Implementation of the results:

Node.js combat 2: Getting Started Module

It can be seen uninstalled successfully.

This reference:

Node.js combat 2: Getting Started Module

Guess you like

Origin www.cnblogs.com/w2sft/p/12008918.html