Remove port number from Mean.js app?

August 6, 2015 3.4k views

After using the one-click Mean.js deployment and adding a domain name, how does one remove the :3000 port number from the address?

2 Answers

In order to bind a program to port 80 or any port under 1024, it needs to have root privileges. Running an app as root is usually a bad idea so what is usually done is running a reverse proxy such as nginx or HAProxy on port 80 that forwards all incoming requests to the MEAN app on port 3000.

First, install nginx:

sudo apt-get update
sudo apt-get install nginx

Then, replace the default server block in /etc/nginx/sites-available/default with the following:

server {
    listen 80;

    server_name example.com;

    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;

Finally, restart nginx:

sudo service nginx restart

Nginx should now be listening on port 80 and forwarding all requests to the MEAN app that is running on port 3000.

If you're running the app in production, you will need to set it up so that it automatically starts on boot and restarts when it crashes. Take a look at the Install PM2 and Manage Application with PM2 sections in the following tutorial:

How To Set Up a Node.js Application for Production on Ubuntu 14.04

Node.js is an open source Javascript runtime environment for easily building server-side and networking applications. The platform runs on Linux, OS X, FreeBSD, and Windows, and its applications are written in JavaScript. Node.js applications can be run at the command line but we will...
  • I finally got around to this. Thank you for your prompt answer, even though it took me 4 months to get to it.

When starting the server set the port NODE_ENV=production PORT=80 node server

Have another answer? Share your knowledge.