Since 1988 When I’m dead.
Run an ExpressJS App (Like KeystoneJS) through Apache and Kill the Port
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.
Adobe: Creative, marketing and document management solutions
Craft CMS | Focused content management for web professionals
Wikipedia, the free encyclopedia
Etsy.com | Shop for anything from creative people everywhere
Global Digital Asset Exchange - GDAX
Bootstrap · The world's most popular mobile-first and responsive front-end framework.
Google Cloud Computing, Hosting Services & APIs | Google Cloud Platform
webpack module bundler
Build software better, together
Phoenix Home Services
Raspberry Pi - Teach, Learn, and Make with Raspberry Pi
Sass: Syntactically Awesome Style Sheets
Stack Overflow - Where Developers Learn, Share, & Build Careers