how to run node.js app from cmd with some predefined port in Windows

前端 未结 4 1092
暖寄归人
暖寄归人 2020-12-18 00:35

Is it some command exist to run my node.js code with directly specified port? something like node server.js port=7777 ?

my server.js code looks like thi

4条回答
  •  醉酒成梦
    2020-12-18 01:02

    Depending on what server.js contains you should be able to do so.

    At a minimum you should read port (you could use https://github.com/substack/node-optimist)

    var argv = require('optimist').argv;
    console.log(argv.port);
    
    // use it like this
    $ node server.js -port 7777
    

    and then listen to it on your server (this depends on what lib you're using).

    Run the server like this

    export PORT=7777; node server.js
    

提交回复
热议问题