Quickly Master the encapsulation and use of the Node. js module and the node. js module Encapsulation
Node. js also has some function encapsulation, which is similar to the C # class library and encapsulated into modules for ease of use. After installation, you can use require () to introduce the call.
I. Node. js module Encapsulation
1. Create a folder named censorify
2. Create three censortext. js, package. json, and README. md files under censorify.
1) enter a function under censortext. js to filter specific words and use asterisks instead.
var censoredWorlds=["sad","bad","mad"];var custormCensoredWords=[];function censor(inStr){ for(idx in censoredWorlds) { inStr=inStr.replace(censoredWorlds[idx],"****"); } for(idx in custormCensoredWords) { inStr=inStr.replace(custormCensoredWords[idx],"****"); } return inStr;} function addCensoreWorld(world){ custormCensoredWords.push(world);} function getCensoreWorlds(){ return censoredWorlds.concat(custormCensoredWords);}exports.censor=censor;exports.addCensoreWorld=addCensoreWorld;exports.getCensoreWorlds=getCensoreWorlds;
2) configure the configuration information in the package, such as the version name and main command.
{ "author":"cuiyanwei", "name":"censority", "version":"0.1.1", "description":"Censors words out of text", "main":"censortext", "dependencies":{ "express":"latest" }, "enginee":{ "node":"*" }}
3) The created README. md file mainly describes
3. Use command line to create an encapsulation Module
Use the command line to navigate to the censorify folder, and then use the command npm pack to encapsulate and generate the tgz file, which encapsulates a module.
Ii. Use of encapsulation modules
The encapsulation module can be published to the NPM registry and used locally. Only local methods are recorded here.
1. Create the readwords folder
2. navigate to the readwords folder through the command line and install the encapsulated module. If it is the NPM install name that has been published to the npm registry, if it is the local npm install tgz file path.
3. After the installation is complete, the node_modules folder containing the censority sub-folder will be generated in the readwords folder.
4. Create a New readwords. js file for testing (note that the Code console and function of the encapsulation module should not be written incorrectly)
var censor=require("censority");console.log(censor.getCensoreWorlds());console.log(censor.censor("Some very sad,bad and mad text"));censor.addCensoreWorld("gloomy");console.log(censor.getCensoreWorlds());console.log(censor.censor("A very goolmy day."));
5. Use
Use the command line node readwords. js to call readwords. js to view the result.
The above is all the content of this article, hoping to help you learn.
Articles you may be interested in:
- How to install the Node. js module on Windows
- Node. js (2) --- Node. js event Module
- Node. js (iii) --- Node. js Module
- Common Methods of NODE. JS encryption module CRYPTO
- Encapsulation of common modules in Node. js
- Node. js module loading details
- Use C/C ++ to implement Node. js modules (1)
- Use C/C ++ to implement Node. js modules (2)
- Node. js uses the require () function to load modules