Mar
Mar

Reputation: 1606

How to assign a domain name to listening port on node.js?

I have a simple app which works on localhost with node.js.

server.js

var port = process.env.PORT || 8080; 

Is it possible to assign a custom url to that port? Exp: localhost:myapp

thanks in advance!

Upvotes: 1

Views: 1819

Answers (1)

Paul
Paul

Reputation: 36349

Sure, but it's a touch convoluted.

First, you'd need something like Apache or NGinx to act as a reverse proxy. Your app still runs at localhost:8080 or whatever, but NGinx can listen to a given host name and route that traffic to an 'upstream server' which in this case is localhost:8080. http://nginx.com/resources/admin-guide/reverse-proxy/

Then you'd have to register that domain name either to a DNS server or for just local development, in your /etc/hosts file. /etc/hosts would be something like this:

127.0.0.1   myapp

Upvotes: 1

Related Questions