[英]how to run node.js app from cmd with some predefined port in Windows
Is it some command exist to run my node.js code with directly specified port?是否存在使用直接指定端口运行我的 node.js 代码的某些命令? something like
node server.js port=7777
?类似于
node server.js port=7777
?
my server.js code looks like this我的 server.js 代码看起来像这样
http.createServer(function (req, res) {
if (mount(req, res)) return;
}).listen(process.env.PORT || 80);
Depending on what server.js
contains you should be able to do so.根据
server.js
包含的内容,您应该能够这样做。
At a minimum you should read port
(you could use https://github.com/substack/node-optimist )至少你应该阅读
port
(你可以使用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
A simple adding to Alberto's answer.对阿尔贝托的答案的简单补充。 On Windows machine you haven't
export
command in cmd
, use set
instead.在 Windows 机器上,您没有在
cmd
export
命令,请改用set
。 Then the whole script will be looks like this:然后整个脚本将如下所示:
set PORT=7777
node server.js
Note that the syntax in PowerShell is slightly different:请注意,PowerShell 中的语法略有不同:
$env:PORT=7777
node server.js
for linux export PORT=5000;对于 linux 导出 PORT=5000; node index.js
节点索引.js
for windows set PORT=5000;对于 Windows 设置 PORT=5000; node index.js
节点索引.js
in case of powerShell在 PowerShell 的情况下
set $env:PORT=5000;设置 $env:PORT=5000; node index.js
节点索引.js
const port = process.env.PORT || const port = process.env.PORT || 3000;
3000; app.listen(port,()=>{console.log(
Listening on port ${port}
)}) app.listen(port,()=>{console.log(
Listening on port ${port}
)})
On ubuntu I just do在 ubuntu 上我只做
PORT=7777 node .
in commandline, no set or export needed.在命令行中,不需要设置或导出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.