Using Symbolic Names for Localhost Ports

Menu

A simple solution to a micro-problem.

The micro-problem: when testing applications running on different ports on localhost, URL completion on the browser is ineffective. In fact, there will be different completions of “localhost”, such as localhost:3000, localhost:4000, localhost:8080, to name a few.

Every time you run a specific service on localhost, thus, you have to type everything on the URL bar, including the port number.

The interaction can be simplified a bit using /etc/hosts. In fact, you can define an alias for each of the services you usually run on localhost.

For instance:

$ cat /etc/hosts
...
127.0.0.1       jekyll
127.0.0.1       rails
...

The first time you run a specific app, invoke it using the symbolic name you defined for that service in /etc/hosts. The browser will require you to specify everything, including the port number. For instance:

http://rails:3000

However, the URL will be stored in the browser history and the next time you will just need to type the first few characters of your aliases to get the full address.

To make it even more effective, use names which are unique from the first characters. For instance, you might use mn for middleman, jkl for jekyll, etc. As long as you do not have any website in your history starting with the same letters, you will need to type only the first two characters.