Fueled on Bacon

FOB
Blog

Expose Port 80 to Node.js

This is an often glossed over detail about deployment: exposing internet ports.

Websites use ports 80 and 443 for regular http/https traffic respectively. But when developing locally, if you try to use one of those ports, you’ll immediately discover that you can’t expose anything less than port 1024 without higher permissions.

If you’re on deploying a server on a Linux VM (which you probably are), you can give Node permissions to run on port 80 with just one line.

 sudo setcap 'cap_net_bind_service=+ep' /path/to/bin/node

Setcap is a Linux utility that sets the capabilities of a file or binary. In this case we’re trying to give the capability to bind internet privileged ports to Node.

If you aren’t sure where your current node binary lives, which is totally possible if you’ve experimented with version managers and stuff, you can find it this way:

which node

So to get a more general version of this command, you could do this:

 sudo setcap 'cap_net_bind_service=+ep' $(which node)

But once you’ve done this, you don’t need to figure out apache or nginx configuration files.

If you’re like me, you’ll probably be so happy with this realization that you just delete apache and nginx from your machines entirely.

Gotchas

You’ll find that `setcap isn’t necessarily included in every distro out of the box. So you might have to install it, and it’s not as obvious as “sudo apt-et install setcap”

It’s actually buried inside “libcap2-bin”

If you find yourself on a distro that doesn’t have it out of the box, try something like this:

sudo apt-get install libcap2-bin

Now you too can live the “Node only” stack life.

Let's get started

Just let us know the basics and we'll send you a Slack invite to discuss with the whole team.

What are we going to be talking about?

%d bloggers like this: