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

2019-01-08 15:28发布

问题:

I use the node.js and socket.io.

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

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

Where and how can I set this setting?

回答1:

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



回答2:

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.

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.



回答3:

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.

<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>


回答4:

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.