detect-port, 端口检测器的JavaScript实现

分享于 

3分钟阅读

GitHub

  繁體 雙語
JavaScript Implementation of Port Detector
  • 源代码名称:detect-port
  • 源代码网址:http://www.github.com/node-modules/detect-port
  • detect-port源代码文档
  • detect-port源代码下载
  • Git URL:
    git://www.github.com/node-modules/detect-port.git
    Git Clone代码到本地:
    git clone http://www.github.com/node-modules/detect-port
    Subversion代码到本地:
    $ svn co --depth empty http://www.github.com/node-modules/detect-port
    Checked out revision 1.
    $ cd repo
    $ svn up trunk
    

    logo

    NPM versionbuild statusTest coveragenpm download

    端口检测器的JavaScript实现

    用法

    
    
    
    
    $ npm i detect-port --save
    
    
    
    
    
    
    
    
    const detect = require('detect-port');
    
    
    
    /**
    
    
     * callback usage
    
    
     */
    
    
    
    detect(port, (err, _port) => {
    
    
     if (err) {
    
    
     console.log(err);
    
    
     }
    
    
    
     if (port == _port) {
    
    
     console.log(`port: ${port} was not occupied`);
    
    
     } else {
    
    
     console.log(`port: ${port} was occupied, try port: ${_port}`);
    
    
     }
    
    
    });
    
    
    
    /**
    
    
     * for a yield syntax instead of callback function implement
    
    
     */
    
    
    
    const co = require('co');
    
    
    
    co(function *() {
    
    
     const _port = yield detect(port);
    
    
    
     if (port == _port) {
    
    
     console.log(`port: ${port} was not occupied`);
    
    
     } else {
    
    
     console.log(`port: ${port} was occupied, try port: ${_port}`);
    
    
     }
    
    
    });
    
    
    
    /**
    
    
     * use as a promise
    
    
     */
    
    
    
    detect(port)
    
    
    . then(_port => {
    
    
     if (port == _port) {
    
    
     console.log(`port: ${port} was not occupied`);
    
    
     } else {
    
    
     console.log(`port: ${port} was occupied, try port: ${_port}`);
    
    
     }
    
    
     })
    
    
    . catch(err => {
    
    
     console.log(err);
    
    
     });
    
    
    
    
    

    命令行 工具

    
    
    
    
    $ npm i detect-port -g
    
    
    
    

    快速启动

    
    
    
    
    # get an available port randomly
    
    
    $ detect
    
    
    
    # detect pointed port
    
    
    $ detect 80
    
    
    
    # output verbose log
    
    
    $ detect --verbose
    
    
    
    # more help
    
    
    $ detect --help
    
    
    
    

    许可证


    相关文章