Node.js中的process.env.PORT是什么? [英] What is process.env.PORT in Node.js?

查看:845
本文介绍了Node.js中的process.env.PORT是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是 process.env.PORT || 3000 用于Node.js?我看到这个地方:

what is process.env.PORT || 3000 used for in Node.js? I saw this somewhere:

 app.set('port', process.env.PORT || 3000);

如果用于将 3000 设置为听力端口,我可以用吗?

If it is used to set 3000 as the listening port, can I use this instead?

app.listen(3000);

如果不是为什么?

推荐答案

在许多环境(例如Heroku)中,作为惯例,您可以设置环境变量 PORT 来告诉您的Web服务器要侦听哪个端口on。

In many environments (e.g. Heroku), and as a convention, you can set the environment variable PORT to tell your web server what port to listen on.

所以 process.env.PORT || 3000 意味着:环境变量PORT中有什么,如果没有什么,则为3000。

So process.env.PORT || 3000 means: whatever is in the environment variable PORT, or 3000 if there's nothing there.

所以你通过 app.listen app.set('port',...),这样您的服务器就可以接受参数从环境中收听哪些端口。

So you pass that app.listen, or to app.set('port', ...), and that makes your server be able to accept a parameter from the environment what port to listen on.

如果您将 3000 硬编码到 app.listen(),你总是听端口3000,这可能只是为你而定,这取决于你的需求和你运行你的环境的要求服务器。

If you pass 3000 hard-coded to app.listen(), you're always listening on port 3000, which might be just for you, or not, depending on your requirements and the requirements of the environment in which you're running your server.

这篇关于Node.js中的process.env.PORT是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆