Node.js - 如何从 url 中删除端口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9526500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:16:03  来源:igfitidea点击:

Node.js - How can I remove the port from the url?

urlnode.jsportsocket.io

提问by Jenan

I use the node.js and socket.io.

我使用 node.js 和 socket.io。

My application runs on the port 3000. The application starts from from the url: mydomain.com:3000/

我的应用程序在端口 3000 上运行。应用程序从 url: mydomain.com:3000/ 开始

I want to run the application from mydomain.com- I want to remove the port from the url.

我想从mydomain.com运行应用程序- 我想从 url 中删除端口。

Where and how can I set this setting?

我在哪里以及如何设置此设置?

采纳答案by sarnold

Find your server.listencall and change the port from 3000to 80. Don't forget that you have to run the program with the CAP_NET_BIND_SERVICEcapability (see capabilities(7)for details) in order to bind to ports less than 1024 on Linux systems. rootprivilege will contain this, and other, privileges.

找到您的server.listen呼叫并将端口从 更改300080。不要忘记,为了绑定到 Linux 系统上小于 1024 的端口,您必须运行具有该CAP_NET_BIND_SERVICE功能的程序(请参阅capabilities(7)有关详细信息)。root特权将包含此特权和其他特权。

回答by Aashay Desai

If you want to run it "without a port" like you describe, you're actually going to be running it on port 80. You can't do this without root permissions.

如果你想像你描述的那样“没有端口”运行它,你实际上将在端口 80 上运行它。没有 root 权限你不能这样做。

So instead of

所以代替

node server.js

You need

你需要

sudo node server.js

This is assuming you have sudo permissions on the machine you're trying to run it on. Otherwise you're going to run into EACCESS problems. That's what sarnold is trying to tell you.

这是假设您在尝试运行它的机器上具有 sudo 权限。否则你会遇到 EACCESS 问题。这就是萨诺德想要告诉你的。

回答by Anna

This is how I did it because I'm using apache as well I can't use port 80 because it reserves it. So, I setup a proxy pass. I set my /src folder to be ignored.

我就是这样做的,因为我也在使用 apache 我不能使用端口 80,因为它保留了它。所以,我设置了一个代理通行证。我将 /src 文件夹设置为被忽略。

<VirtualHost *:80>
    DocumentRoot "/Library/WebServer/Documents/www.mysite.com"
    ServerName local.www.mysite.com
    ServerAlias local.www.mysite.com
    ProxyPass /src !
    ProxyPass / http://local.www.mysite.com:3000/
    ProxyPassReverse / http://local.www.mysite.com:3000/
</VirtualHost>

回答by feralcreature

First, you probably want to be running the application from your localhost, "127.0.0.1". You can remove the port just by omitting it in the node.js setup. By default, web browsers look for servers on port 80. If you want your server running on port 3000 for some reason, it will have to be included in the URL.

首先,您可能希望从本地主机“127.0.0.1”运行应用程序。您可以通过在 node.js 设置中省略它来删除端口。默认情况下,Web 浏览器在端口 80 上查找服务器。如果出于某种原因您希望服务器在端口 3000 上运行,则必须将其包含在 URL 中。