国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

Node.js模塊封裝及使用方法

2019-11-20 10:26:41
字體:
供稿:網(wǎng)友

Node.js中也有一些功能的封裝,類似C#的類庫,封裝成模塊這樣方便使用,安裝之后用require()就能引入調(diào)用.

一、Node.js模塊封裝

 1.創(chuàng)建一個名為censorify的文件夾

 2.在censorify下創(chuàng)建3個文件censortext.js、package.json、README.md文件

1)、在censortext.js下輸入一個過濾特定單詞并用星號代替的函數(shù)。

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)、在package中配置清單信息 例如版本 名稱和main指令等。

{ "author":"cuiyanwei", "name":"censority", "version":"0.1.1", "description":"Censors words out of text", "main":"censortext", "dependencies":{ "express":"latest" }, "enginee":{ "node":"*" }}

3)、創(chuàng)建的README.md文件主要是描述說明

3.使用命令行創(chuàng)建封裝模塊

使用命令行導(dǎo)航到censorify文件夾下,然后使用命令 npm pack 封裝生成tgz文件,這樣就封裝了一個模塊。

二、封裝模塊的使用

封裝模塊的使用有兩種方法 :發(fā)布到NPM注冊表、本地使用,這里只記錄下本地使用的方法.

1.創(chuàng)建名readwords文件夾

2.命令行導(dǎo)航到readwords文件夾下,然后安裝已經(jīng)封裝好的模塊,如果是已經(jīng)發(fā)布到NPM注冊表的直接 npm install 名字,如果是在本地 npm install tgz文件路徑。

3.安裝完成后會在readwords文件夾下生成包含censority子文件夾的node_modules文件夾

4.新建readwords.js文件測試(注意代碼console、封裝模塊的函數(shù)別寫錯了)

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.使用

用命令行node readwords.js來調(diào)用readwords.js查看結(jié)果

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 灌阳县| 宜兴市| 调兵山市| 龙井市| 石河子市| 镇原县| 扶余县| 铜山县| 新民市| 十堰市| 吉木乃县| 达拉特旗| 石景山区| 蕉岭县| 东城区| 贵南县| 九寨沟县| 勐海县| 龙门县| 大埔区| 灵璧县| 凉山| 新泰市| 佛坪县| 常州市| 梅河口市| 长武县| 阳泉市| 炉霍县| 乌恰县| 红桥区| 乌兰县| 凌云县| 宜宾县| 东兰县| 海淀区| 敦化市| 安陆市| 克拉玛依市| 扎鲁特旗| 修武县|