为什么在开发环境中为 Express 使用静态端口,而在生产环境中不使用?

huangapple go评论74阅读模式
英文:

Why use a static port for express in dev environment but not in production?

问题

以下是翻译好的内容:

已经查看了多个来源(即教程和示例),它们都指出启动服务器的方法如下:

const port = process.env.PORT || 5000;
app.listen(port, () => {
  console.log(`Server is listening on port ${port}`);
});

但是在部署时,你会写成以下形式(这也是部署服务“设置Node.js应用程序”的建议,似乎是一个常见的工具):

app.listen();

自然而然,我会像下面这样做来解决这个问题:

if (process.env.NODE_ENV == "prod") {
  app.listen(0, () => {
    console.log("prod");
  });
} else {
  app.listen(process.env.PORT, () => {
    console.log(`Server is listening on port ${port}`);
  });
}

但是,在本地主机上测试“prod”环境时,脚本编译成功,但在Postman中出现ECONNREFUSED错误。为什么在开发环境中需要设置端口,但在生产环境中不应该设置任何静态内容呢?我个人分析的原因是最佳实践的方法,但由于部署工具也警告了这一点,使用listen()中不带参数会失败,所以我对人们为什么这样做感到有些不确定。

英文:

Been looking around and multiple sources (i.e tutorials and examples) state that you start a server like this:

const port = process.env.PORT || 5000;
app.listen(port, () => {
  console.log(`Server is listening on port ${port}`);
});

But when deploying you write like this below: (which is also stated by the deploy service "Setup Node.js App" which seems to be a common tool)

app.listen();

Naturally, I'd use something like below to combat this issue:

if (process.env.NODE_ENV == "prod") {
  app.listen(0, () => {
    console.log("prod");
  });
} else {
  app.listen(process.env.PORT, () => {
    console.log(`Server is listening on port ${port}`);
  });
}

But when testing the 'prod' environment on localhost the script compiles well but I get ECONNREFUSED in Postman. Why do I need to setup a port while on dev but I'm supposed to not set anything static in production? My own analysis on that is because of a best-practice method but since the deployment tool also warns about it and using no parameters in listen() fails - I get a bit unsure of why people are doing this.

答案1

得分: 2

在开发过程中,静态端口很方便,因为您始终知道可以在本地计算机上访问它的位置。唯一的问题是,您指定的端口不能已经被占用。

当发布到云提供商时,您不知道应该运行在哪个端口。要么服务器让Express自行选择,要么选择最适合它的端口。

如果您想通过Postman访问生产版本,您必须知道Express选择的端口。您可以这样获取它:

let server = app.listen(0, () => {
  console.log(`Example app listening on port`, server.address().port);
});
英文:

When you are developing, a static port is nice because you always know where to reach it on your local machine. The only thing is that the port you specify must not be taken already.

When publishing to a cloud provider, you don't know what port the is should run on. Either the server lets Express choose it's own, or chooses a port that suits him best.

If you want to reach the prod version via Postman, you have to know the port chosen by Express. You can get it like this:

let server = app.listen(0, () => {
  console.log(`Example app listening on port`, server.address().port);
});

huangapple
  • 本文由 发表于 2023年1月4日 21:41:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75006351.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定