nodejs之初

來源:互聯網
上載者:User

標籤:move   替換   註冊表   require   rds   auth   view   發布   author   

包管理器npm

常用命令列選項有

  1. search:在儲存庫裡面尋找模組包;——npm search express
  2. install:使用在存放庫或本地位置上的一個package.json檔案來安裝包;——npm install eapress;npm install [email protected];npm install ../abc.tgz
  3. install -g:在全域可訪問位置安裝一個包;——npm install -g express;
  4. remove:刪除一個模組;npm remove express
  5. pack:把在一個package.json檔案中定義的模組封裝成.tgz檔案;——npm pack
  6. view:顯示模組的詳細資料;——npm view express
  7. publish:把在一個package.json檔案中定義的模組發布到註冊表上;——npm publish
  8. unpublish:取消你發行的模組;——npm unpublish myModule
使用package.json檔案

所有的模組在他的根目錄下必須包含一個package.json檔案。他是定義一個模組,包括其依賴關係的一個簡單的JSON檔案包。該package.json檔案包含多個指令告訴Node管理器如何處理模組。例如:

{    "name":"my_module",    "version":"0.1.0",    "description":"a simple node.js module",    "dependencies":{            "express":"latest"            }}
建立node.js模組封裝
  1. 建立一個名為.../censorify的專案檔夾。這將是此包的目錄。
  2. 在該檔案中建立一個名為censortext.js的檔案。
  3.  1 var censoredWords=["sad","bad","mad"]; 2 var customCensoredWords=[]; 3 function censor(inStr){ 4     for(idx in censoredWords){ 5         inStr=inStr.replace(censoredWords[idx],"****"); 6         } 7         for(idx in customCensoredWords){ 8             inStr=inStr.replace(customCensoredWords[idx],"****"); 9         }10         return inStr;11     }12 13     function addCensoredWord (word) {14         customCensoredWords.push(word);15     }16     function getCensoredWords(){17         return censoredWords.concat(customCensoredWords);18     }19 20 exports.censor=censor;21 exports.addCensoredWord=addCensoredWord;22 exports.getCensoredWords=getCensoredWords;

     

  4. 在.../censorify檔案夾中建立一個package.json檔案。
     1 { 2     "author":"zhouruxian", 3     "name":"censorify", 4     "version":"0.1.1", 5     "description":"hi", 6     "main":"censortext", 7     "dependencies":{}, 8     "engines":{ 9         "node":"*"10     }11 }

     

  5. 在控制台導航到.../censorify檔案夾下執行

npm pack

npm pack會在.../censorify檔案夾中產生censorify-0.1.1.tgz檔案。

在node.js程式中使用node.js封裝模組
  1. 建立一個專案檔夾名稱.../readwords
  2. 在控制台進入該檔案夾下執行npm install ../censorrify/censorify-0.1.1.tgz
  3. 建立一個名為.../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. 執行命令 Node readwords.js

結果顯示一些字串被“****”替換了。

nodejs之初

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.