NodeJS4-8闈欐€佽祫婧愭湇鍔″櫒瀹炴垬_鏋勫缓cli宸ュ叿

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NodeJS4-8闈欐€佽祫婧愭湇鍔″櫒瀹炴垬_鏋勫缓cli宸ュ叿相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e5%b0%b1%e6%98%af' title='灏辨槸'>灏辨槸   where   ESS   roo   help   span   win32   des   src   

銆€銆€Cli(command-line interface),涓枃鏄?鍛戒护琛岀晫闈?绠€鍗曟潵璇村氨鏄彲浠ラ€氳繃鍛戒护琛屽揩閫熺敓鎴愯嚜宸辩殑椤圭洰妯℃澘绛夊姛鑳?姣旇緝鐔熸倝鐨勬槸vue-cli鑴氭墜鏋惰繖浜?,鎶婁笂杩板啓鐨勫寘鍋氭垚Cli宸ュ叿.

寤剁画涓婁竴娆$殑浠g爜,杩欐鐜板湪readme.md閲岄潰鍏堟敞閲婂憡璇夌敤鎴峰張閭d簺鍛戒护琛岀殑璇彞鍏蜂綋瀵瑰簲鍝簺鍔熻兘

step1:README.md

# anydoor
Tiny NodeJS Static Web server
##瀹夎

```
npm i -g anydoor
```
##浣跨敤鏂规硶

```
anydoor # 鎶婂綋鍓嶆枃浠跺す浣滀负闈欐€佽祫婧愭湇鍔″櫒鏍圭洰褰?
anydoor -p 3333 # 璁剧疆绔彛鍙蜂负 3333 //鎵嬪伐鑷畾涔変竴涓鍙e彿
anydoor -h localhost # 璁剧疆host涓簂ocalhost
anydoor -d /usr # 璁剧疆鏍圭洰褰曚负 /usr

```

Step2:瀹夎yargs

yargs鏄粈涔? Yargs鏄€氳繃瑙f瀽鍙傛暟鍜岀敓鎴愪紭闆呯殑鐢ㄦ埛鐣岄潰锛屽府鍔╂偍鏋勫缓浜や簰寮忓懡浠よ宸ュ叿銆?/p>

瀹夎yargs

npm i yargs

鏂板缓涓€涓猻rc/index.js,涓巃pp.js澶勪簬骞崇骇鐩綍涓?娉ㄦ剰涓嬮潰鎵€瀹氫箟鐨刼ption鏄笌defaultConfig鐨勫搴旂殑鍐呭

// process.argv1
// yargs
const yargs = require(鈥榶args鈥?span style="color: #000000;">)
const Server = require(鈥?/app鈥?span style="color: #000000;">)

const argv = yargs
    .usage(鈥榓nywhere [options]鈥?span style="color: #000000;">)
    .option(鈥榩鈥?span style="color: #000000;">,
        alias:鈥榩ort鈥?span style="color: #000000;">,
        describe:鈥樼鍙e彿鈥?span style="color: #000000;">,
        default:9527
    )
    .option(鈥榟鈥?span style="color: #000000;">,
        alias:鈥榟ostname鈥?span style="color: #000000;">,
        describe:鈥榟ost鈥?span style="color: #000000;">,
        default:鈥?27.0.0.1鈥?span style="color: #000000;">
    )
    .option(鈥榙鈥?span style="color: #000000;">,
        alias:鈥榬oot鈥?span style="color: #000000;">,
        describe:鈥榬oot path鈥?span style="color: #000000;">,
        default:process.cwd()
    )
    .version()
    .alias(鈥榲鈥?鈥榲ersion鈥?span style="color: #000000;">)
    .help()
    .argv;

    const server = new Server(argv)
    server.start()

 

app.js

const http = require(鈥榟ttp鈥?span style="color: #000000;">);
const chalk = require(鈥榗halk鈥?span style="color: #000000;">)
const  path = require(鈥榩ath鈥?span style="color: #000000;">)
const conf = require(鈥?/config/defaultConfig.js鈥?span style="color: #000000;">);
const route = require(鈥?/helper/route鈥?span style="color: #000000;">)

class Server 
    constructor(config)
        this.conf = Object.assign(,conf,config)
    
    start()
        const server = http.createServer((req,res)=>
            const url = req.url;
            //鎷垮埌鏂囦欢璺緞
            const filePath = path.join(this.conf.root,req.url)
            route(req,res,filePath,this.conf)
        );
        
        server.listen(this.conf.port,this.conf.hostname,()=>
            const addr = `http://$this.conf.hostname:$this.conf.port`;
            console.log(`Server started at $chalk.green(addr)`);  
        )
    


module.exports = Server

 

鎶€鏈浘鐗? src=

 package.json娣诲姞main鍜宐in涓や釜閫夐」


  "devDependencies": 
    "eslint": "^6.2.2"
  ,
  "name":"anydoor",
  "version":"0.01",
  "main":"src/app.js",
  "bin":
    "anydoor":"bin/anydoor"
  

 

鏂板缓anydoor/bin/anydoor 

#! /usr/bin/env node

require(鈥?./src/index鈥?

 

杩愯浠g爜,鎴愬姛鍚姩鏈嶅姟,鎵撳紑http://127.0.0.1:9999鍙互鐪嬪埌涔嬪墠杩愯app.js鐨勯〉闈?/p>

鎶€鏈浘鐗? src=

 

 --------------------------------------------------------------------------------------------------------------------

浼樺寲鑷姩鎵撳紑娴忚鍣ㄦ搷浣?涓嬮潰淇敼涔嬪悗杩愯涔嬪悗浼氳嚜鍔ㄦ墦寮€缃戦〉,闈炲父鏂逛究

鏂板缓src/helper/openUrl.js

const exec = require(鈥榗hild_process鈥?span style="color: #000000;">)

module.exports = url=>
    switch(process.platform)
        case 鈥榙arwin鈥?span style="color: #000000;">:
            exec(`open $url`);
            break
        case 鈥榳in32鈥?span style="color: #000000;">:
            exec(`start $url`);
            break
    

 

src/app.js

const http = require(鈥榟ttp鈥?span style="color: #000000;">);
const chalk = require(鈥榗halk鈥?span style="color: #000000;">)
const  path = require(鈥榩ath鈥?span style="color: #000000;">)
const conf = require(鈥?/config/defaultConfig.js鈥?span style="color: #000000;">);
const route = require(鈥?/helper/route鈥?span style="color: #000000;">)
const openUrl = require(鈥?/helper/openUrl鈥?span style="color: #000000;">)

class Server 
    constructor(config)
        this.conf = Object.assign(,conf,config)
    
    start()
        const server = http.createServer((req,res)=>
            const url = req.url;
            //鎷垮埌鏂囦欢璺緞
            const filePath = path.join(this.conf.root,req.url)
            route(req,res,filePath,this.conf)
        );
        
        server.listen(this.conf.port,this.conf.hostname,()=>
            const addr = `http://$this.conf.hostname:$this.conf.port`;
            console.log(`Server started at $chalk.green(addr)`); 
            openUrl(addr) 
        )
    


module.exports = Server

 

以上是关于NodeJS4-8闈欐€佽祫婧愭湇鍔″櫒瀹炴垬_鏋勫缓cli宸ュ叿的主要内容,如果未能解决你的问题,请参考以下文章

鐧藉珫鐗?2019Java寮€鍙戝叏缁堢瀹炴垬绉熸埧椤圭洰瑙嗛鏁欑▼

Android 寮€婧愭鏋?( 涓?) 浜嬩欢鎬荤嚎---EventBus

寰湇鍔″涔犱箣thrift浠嬬粛瀹夎

2020-2021骞翠箣闂?65+寮€鍙戣祫婧愭暣鐞?/a>

Redis 绉掓潃瀹炴垬