changing the name of localhost web app express?

Solution 1:

as you are on development environment, its enough to add

127.0.0.1  testurl.test

on the bottom of

C:\Windows\System32\drivers\etc\hosts

now to set the port of the server to 80 (standartport for http) you need to change

project\bin\www

(ejs standart line 15)

var port = normalizePort(process.env.PORT || '3000');

to

var port = normalizePort(process.env.PORT || '80');

now, if you type testurl.test in your browser, it should show you the website.

this works for windows, if you have an mac or linux, just change the hosts file as you need.

Marius

Solution 2:

You can't change that from within express. The domain name is resolved by the browser using OS system and DNS services. It is not resolved by your express application. It is resolved by the browser before any connection to your express application is made.

You could edit the host file on your local computer to define someothername as an alias for localhost. How exactly you edit the host file depends upon what OS you are running as this is an OS-specific feature.


Just to review, here's the normal steps when you request a web page in the browser.

  1. User requests a specific URL in a browser.
  2. Browser parses the domain out of the URL and requests a DNS lookup of that domain using local TCP-based services on the host OS.
  3. The host OS looks at the domain requested to see if it is a hostname that it recognizes or has a cached entry for.
  4. If it does recognize the hostname or has a cache entry for it, then an IP address corresponding to that hostname is returned back to the browser.
  5. If it does not recognize the hostname, then the local computer contacts a DNS server to lookup the IP address and returns that IP address back to the browser.
  6. Once the browser has the IP address, it makes a TCP connection to that IP address on the desired port number.
  7. If the IP address corresponds to your Express server, then this is the first time that your Express server is even involved in the process.