3

my web application runs on port 3000. if anybody wants to access, they need to access through http://abcdomain.com:3000/. I want to make the application accessible at http://abcdomain.com/.

I want to change default port to specific port (3000) which my application is running

i have changed DNS in linux but iam unable to hide port number on URL

how to achieve this

Jan
  • 31
  • 3
  • 8
  • Possible duplicate... https://stackoverflow.com/questions/16573668/best-practices-when-running-node-js-with-port-80-ubuntu-linode – An0nC0d3r Dec 21 '17 at 23:07

1 Answers1

3

I want to change default port to specific port (3000) which my application is running

You can't change the default port. That is decided by the browser. When the browser sees a URL without a port, it picks the default port of 80 (for http) or 443 (for https) and uses that port. This is not something that can be controlled from the server.

You can, however field a request on the port 80 on your server and then handle it from there. Here are some of your choices:

  1. Run your server on port 80 (the default http port) so no port number is needed in the URL and no redirection or forwarding is needed on the server.

  2. Set up automatic port forwarding on your Linux server using iptables so that incoming port 80 requests are automatically routed to port 3000. Here's one answer that shows how to do that. So, the user uses a URL with a port number which will arrive on your server at port 80, then the iptable setting in your server will forward that to port 3000.

  3. Use some other type of infrastructure in front of your web server such as an NGINX proxy or a load balancer that can forward port 80 requests to port 3000 where your server is listening.

FYI, I use option 2 on my Raspberry Pi Linux node.js server because running on a higher port number, lets me run my server without elevated privileges (which is good for security), yet the iptable port forwarding lets users access the server without a port number.


A redirect server (like you are asking for in comments) seems like the worst option, but you could implement it like this:

// set up plain express server
const app = require('express')();

// set up route to redirect all routes to port 3000
// note this only works for GET requests, things like POST from your code or forms 
//   must specify the proper port 3000
app.get('*', function(req, res) {  
    res.redirect('http:3000//' + req.headers.host + req.url);
});

// have it listen on 80
app.listen(80);
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • My company does not want to use option2 . they are suggested me to use http redirect. iam not sure how to use http redirect? – Jan Dec 25 '17 at 18:38
  • @Jan - Using http redirect would require running another web server on port 80 that redirects any incoming requests to port 3000 and then `:3000` would then show in the user's browser. That is the LEAST desirable solution by far. – jfriend00 Dec 25 '17 at 20:11
  • Can you please let me know how can i do this ? do you have any working code for this ? – Jan Dec 25 '17 at 20:48
  • @Jan - You appear to be pursuing the worst and most inefficient option, but I added an example to my answer. – jfriend00 Dec 25 '17 at 21:16