The beginning of Nodejs

Source: Internet
Author: User

Package Manager NPM

Common command-line options are

    1. Search: find the module package in the REPOSITORY;--NPM search Express
    2. Install: installs the package using a Package.json file in the repository or local location,--npm install eapress;npm install [email protected];npm install. /abc.tgz
    3. Install-g: install a package in a globally accessible location;--npm install-g express;
    4. Remove: Delete a module; npm remove Express
    5. Pack: encapsulates the module defined in a Package.json file into A. tgz file;--npm pack
    6. View: Show details of the MODULE;--NPM View Express
    7. Publish: publish the module defined in a Package.json file to the REGISTRY;--NPM publish
    8. Unpublish: Cancel the module you have PUBLISHED;--NPM unpublish mymodule
Using the Package.json file

All modules must contain a Package.json file in his root directory. He is a simple JSON file package that defines a module, including its dependencies. The Package.json file contains several instructions that tell the node manager how to handle the Module. For example:

{    "name": "my_module",    "version": "0.1.0",    "description": "a simple Node. js Module ",    " dependencies ": {            " Express ":" latest "            }}
Create a Node. JS module encapsulation
  1. Create a project folder Named./censorify. This will be the directory for this Package.
  2. Create a file in the file named Censortext.js.
  3. 1 varcensoredwords=["sad", "bad", "mad"];2 varcustomcensoredwords=[];3 functioncensor (inStr) {4      for(idxinchCensoredwords) {5Instr=instr.replace (censoredwords[idx], "* * *");6         }7          for(idxinchCustomcensoredwords) {8Instr=instr.replace (customcensoredwords[idx], "* * *");9         }Ten         returninStr; one     } a  -     functionAddcensoredword (word) { - Customcensoredwords.push (word); the     } -     functiongetcensoredwords () { -         returnCensoredwords.concat (customcensoredwords); -     } +  -Exports.censor=censor; +exports.addcensoredword=addcensoredword; aexports.getcensoredwords=getcensoredwords;

  4. Create a Package.json file in The./censorify folder.
    1 {  2     "author": "zhouruxian",  3     "name": "censorify",  4     "version": "0.1.1",  5     "description": "hi",  6     "main": " Censortext ",  7     " dependencies ": {},  8     " Engines ": {  9         "node": "*"ten    }

  5. In the console navigate to the .../censorify folder to execute

NPM Pack

The NPM pack will generate the Censorify-0.1.1.tgz file in The./censorify folder.

Using the Node. JS wrapper module in a Node. JS program
    1. Create a project folder name .../readwords
    2. Under the console, go to the folder to perform NPM Install. /censorrify/censorify-0.1.1.tgz
    3. Create a file Named../readwords/readwords.js
      1 var censor=require ("censorify"); 2 console.log (censor.getcensoredwords ()); 3 console.log (censor.censor ("Some very sad,badand mad Text.") )); 4 censor.addcensoredword ("gloomy"); 5 console.log (censor.getcensoredwords ()); 6 console.log (censor.censor ("a very gloomy day"));

    4. Execute command Node readwords.js

The results show that some strings have been replaced by "* * *".

At the beginning of Nodejs

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.