Operationalizing a Node app: Webpack to Forever

When you set up your node application, you likely need to run the bundling of Javascript files as part of the release process.

webpack
mv bundle.js public/search/

The most fiddly bit of this is getting paths correct.

When I deployed my application, I put it at “/search/ssl_search”, so it could hang off this domain. This means that the application lives in a folder named “ssl_search” on the file system, but the “public” folder in Express.js needs to replicate the folder structure “/search” to be able to serve files correctly.

To get this to work, you need to set up apache to forward ports for these paths:

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so


      ServerAdmin gary.sieling@gmail.com
      ServerName garysieling.com
      ServerAlias www.garysieling.com
      DocumentRoot /srv/www/garysieling.com/public_html/
      ErrorLog /var/log/apache2/garysieling-error.log
      CustomLog /var/log/apache2/garysieling-access.log combined

      ProxyRequests Off
      ProxyErrorOverride Off

      
        ProxyPass "http://127.0.0.1:3000/search"
      

      ProxyPassReverse "/search" "http://localhost:3000/search"

If you screw this up (ProxyRequests On) you will turn your server into an open relay proxy for people (forward proxy), which is not a great idea (you want a reverse proxy).

You may need to enable the proxy modules:

a2enmod proxy
a2enmod proxy_http

I saw some documentation which recommended a hard stop of apache, to get these to really take (not sure how necessary this is):

/etc/init.d/apache2 stop && /etc/init.d/apache2 start

You can use “Forever” to keep a node application running:

npm install forever --save
node_modules/forever/bin/forever start \
 -append \
 --spinSleepTime 100 \
 --minUptime 200 \
 -l /var/log/ssl-search-forever.log \
 -o /var/log/ssl-search-output.log \
 -e /var/log/ssl-search-error.log \
 index.js

node_modules/forever/bin/forever list

There are a few caveats with logging – to get the right access log format, you need something like this:

app.use(require("morgan")("combined"));

There are a couple ways to handle log rotation – it looks like you can use features built into morgan, or you can use general purpose logrotate.d which is what I did:

/var/log/ssl-search*.log {
  daily
  dateext
  missingok
  rotate 7
  compress
  delaycompress
  notifempty
  copytruncate
}

CDN
If you’re using a CDN (e.g. Cloudflare) there are a few things to note as well:

When you’re working on this, you’ll need to remember to disable caching.

Cloudflare can add SSL in front for you, so if you have hard-coded URLs in your app, you should probably remove them to make your life simpler.

Cloudflare has an interesting set of caching options:

If you want it to cache the most aggressively, you can have it cache every page. Depending on what you’re trying to do, you may want to design the application to avoid query strings (?x=12345) for different pages, to make this simpler.

SEO
You may also want to make a sitemap in your node app and register it with Google Webmaster Tools / Bing Webmaster Tools.

Leave a Reply

Your email address will not be published. Required fields are marked *