How to run multiple Node apps on the same host without exposing ports

For your personal website, you may want to host multiple Node apps that run on different port numbers on the same host. Of course, you can access each application directly by its port number, but this is inconvenient and insecure. A better approach is to use a proxy server that directs traffic to each of your Node apps behind the scenes.

In this post I'll show you how to set up a simple proxy server in 5 minutes. Let's get started!

Example

I run a Node application called Fit Bank on this website. (The site is hosted by Digital Ocean, but you could easily do this with AWS or any other provider that gives you a host with SSH access.) Let's say that Fit Bank runs on port 3456. Until recently, I used a link like this to access it:

// The old, frustrating link to my side project
http://codyromano.com:3456  

The port number is annoying to type and also insecure; many corporate networks will block traffic to external ports except for standard ones like 80. I wanted to access Fit Bank like this:

// Much better!
http://codyromano.com/fit-bank/  

Set up your own simple proxy server for Node projects

To run multiple Node apps on the same host without exposing port numbers, I made a simple app called Personal Proxy Server. A wrapper on top of Node's http-proxy, it provides the following benefits:

  1. You can add new side projects to your website without writing proxy-server code. Instead of writing code to register new URL paths, you update a JSON file.
  2. You can easily designate a default application to handle all other requests. In the case of this website, I use a Ghost blog as the default app.

Install

git clone https://github.com/codyromano/personal-proxy-server.git  
cd personal-proxy-server && npm install  

Configure

  1. Edit the list of applications in config/applications.json. (You'll see the example involving Fit Bank.)
  2. Define your default app in config/defaultApplication.json.
  3. Update the paths in config/paths.json according to your environment.

Run

First, start the applications that you defined in config/defaultApplications.json. Once the servers are running, you can fire up the proxy server itself:

npm start  
open http://yourdomain  

Summary

Using a proxy server in Node lets you run multiple applications (e.g. side projects) on one host without exposing port numbers. My Personal Proxy Server makes it a little simpler: letting you add, edit and remove side projects by updating a config file.

Note: This isn't the ideal system design. If you're expecting heavy traffic or dealing with sensitive data, seriously consider TLS, load balancing and reverse proxies. But if you're hosting a blog or simple side projects where convenience is king, Personal Proxy Server may be a good fit.