在不使用端口的情况下运行Node.js应用程序。

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

Run node js application without port

问题

我想在当前IP地址上的端口80上运行我的应用程序,而不分配任何端口。这如何可能。

英文:

I wants to run my application without assigning any port on my current IP address on port 80. How it will possible.

答案1

得分: 11

如果你的Node.js应用程序是一个Web服务器,你不能移除端口。没有端口就没有Web服务器。试图创建一个没有端口的Web服务器就像试图创建一个没有铁路的火车。

然而,你可以使用默认端口。当用户在浏览器中输入没有端口的URL时,浏览器会自动应用默认端口。对于像http://example.com/http://10.11.12.13/这样的URL,默认端口是80。对于https://example.com,默认端口是443,你需要使用https服务器类。

因此,你可以让你的服务器监听端口80。

在开发过程中,你会遇到一个问题。在OSX、Linux和其他类UNIX操作系统上,只有特权用户(root用户)才能运行使用小于1024的端口号的服务器。典型的开发周期,即编辑/运行/测试,在需要特权才能运行时会带来很大的麻烦,也存在安全隐患。这就是为什么Node.js示例使用端口3000的原因。

在生产环境中,许多人使用nginx作为反向代理服务器,将来自端口80的http请求或来自端口443的https请求中继到Node.js服务器的端口3000。你可以阅读如何做到这一点;这远远超出了Stack Overflow答案的范围。

英文:

If your node.js application is a web server, you cannot remove the port. No port, no web server. Trying to make a web server without a port is like trying to make a locomotive with no railroad.

You can use the default port however. When users give browsers URLs without ports, they automatically apply the default port. For URLs like http://example.com/ or http://10.11.12.13/ the default port is 80. For https://example.com it's 443, and you need to use the https server class.

So, you can make your server listen on port 80.

In development you will run into a problem with this approach. On OSX, Linux, and other UNIX-derived OSs, only the privileged (root) user can run servers that use port numbers less than 1024. The typical development cycle of edit / run / test is a huge hassle, and a security hole, when you need privileges to run. That's why the node.js examples use port 3000.

In production, many people use nginx as a reverse proxy server to relay http requests from port 80 or https requests from port 443 to your node.js server at port 3000. You can read about how to do that; it's far beyond the scope of a Stack Overflow answer,

huangapple
  • 本文由 发表于 2020年1月6日 20:02:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/59611764.html
匿名

发表评论

匿名网友

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

确定