APIs
noobguru
5 years
4 years
CMSes
noobguru
4 years
1 year
17 years
10 years
12 years
Databases
noobguru
2 years
14 years
Frameworks
noobguru
4 years
2 years
5 years
1 year
12 years
3 years
1 year
1 year
3 years
4 years
5 years
Hobbies
noobguru
5 years
1 year
26 years
1 year
3 years
Languages
noobguru
18 years
12 years
14 years
7 years
14 years
14 years
2 years
Methodologies
noobguru
6 years
3 years
2 years
2 years
14 years
6 years
5 years
14 years
3 years
1 year
Software
noobguru
13 years
24 years
3 years
15 years
4 years
Stacks
noobguru
14 years
1 year
Systems
noobguru
14 years
2 years
6 years
4 years
1 year
1 year
1 year
3 years
Tools
noobguru
3 years
1 year
4 years
4 years
1 year
1 year
Jonny Asmar
Front End & Full Stack Web Developer
 
 
UI/UX Obsessor NoVA
Since 1988 When I’m dead.

Run an ExpressJS App (Like KeystoneJS) through Apache and Kill the Port

June 5, 2017 How-ToExpressJS, KeystoneJS, NodeJS, LAMP, Apache, Systems

If you use ExpressJS or anything built on it (like KeystoneJS, which this site is very proudly built upon) you've probably encountered the many examples of running these apps with a port. Typical examples will suggest you do something to this effect:

This is fine and dandy and I don't suggest you do anything different. However, for a plethora of reasons, you really don't want to require your visitors to enter a port to access your site. jonnyasmar.com:3000 just doesn't have the same appeal as jonnyasmar.com, does it?

The solution here is really very simple. Use a proper HTTP server to handle requests to your site. My HTTP server of choice tends to be Apache and so for the purposes of this post, I'll use that. Many of you may prefer Nginx and this method can be used there as well, but I won't get into that here. My Nginx experience is limited, so I'll leave that to the experts.

Let's Make Sure Your Server Stays Up (with ForeverJS)

The first order of business is to make sure your ExpressJS server is reliable. Errors & exceptions thrown by a NodeJS app (of which, ExpressJS is) will cause the script to stop execution, thus throwing your ExpressJS server offline. Several other factors can cause this as well. So, for jonnyasmar.com I've decided to load my server call with forever (https://github.com/foreverjs/forever). ForeverJS is a very simple NodeJS app that automatically restarts your code when it fails.

To install forever:

npm install forever -g

With that out of the way, you can now load up your code like so:

forever start my-super-awesome-express-site.js

Easy as that! I also recommend checking out some of the other useful forever features like list and logs. Now that you've ensured your server will restart if it crashes for some reason or another, we can worry about getting rid of that pesky port that you're running on.

ProxyPass with Apache

This piece is almost just as easy as the last. First, you'll need to open up your Apache's Virtual Hosts config. Mine sits at /etc/apache2/sites-available/000-default.conf, though this does differe by environment.

Next, you'll need to add a VirtualHost directive that proxies a domain name to the port of your ExpressJS server. This is super-simple as well:

And that's it. Restart Apache with some version of service apache2 restart and you're good to go. Your domain will now proxy to port 3000 of the same machine. Note that ProxyPass can even be used to proxy to a totally different domain name. For our purposes, this is unnecessary, but it's a good tidbit to keep in mind.



Thank you for reading! Please don't hesitate to leave any feedback in the comments section below or if you prefer private communication you can also find a link to email me at the very top right corner of this page.

APIs
noobguru
5 years
4 years
CMSes
noobguru
4 years
1 year
17 years
10 years
12 years
Databases
noobguru
2 years
14 years
Frameworks
noobguru
4 years
2 years
5 years
1 year
12 years
3 years
1 year
1 year
3 years
4 years
5 years
Hobbies
noobguru
5 years
1 year
26 years
1 year
3 years
Languages
noobguru
18 years
12 years
14 years
7 years
14 years
14 years
2 years
Methodologies
noobguru
6 years
3 years
2 years
2 years
14 years
6 years
5 years
14 years
3 years
1 year
Software
noobguru
13 years
24 years
3 years
15 years
4 years
Stacks
noobguru
14 years
1 year
Systems
noobguru
14 years
2 years
6 years
4 years
1 year
1 year
1 year
3 years
Tools
noobguru
3 years
1 year
4 years
4 years
1 year
1 year