{"id":3817,"date":"2016-04-19T12:39:33","date_gmt":"2016-04-19T12:39:33","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=3817"},"modified":"2016-04-19T12:39:33","modified_gmt":"2016-04-19T12:39:33","slug":"operationalizing-node-app-webpack-forever","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/operationalizing-node-app-webpack-forever\/","title":{"rendered":"Operationalizing a Node app: Webpack to Forever"},"content":{"rendered":"<p>When you set up your node application, you likely need to run the bundling of Javascript files as part of the release process. <\/p>\n<pre lang=\"javascript\">\nwebpack\nmv bundle.js public\/search\/\n<\/pre>\n<p>The most fiddly bit of this is getting paths correct.<\/p>\n<p>When I deployed my application, I put it at &#8220;\/search\/ssl_search&#8221;, so it could hang off this domain. This means that the application lives in a folder named &#8220;ssl_search&#8221; on the file system, but the &#8220;public&#8221; folder in Express.js needs to replicate the folder structure &#8220;\/search&#8221; to be able to serve files correctly.<\/p>\n<p>To get this to work, you need to set up apache to forward ports for these paths:<\/p>\n<pre lang=\"xml\">\nLoadModule proxy_module modules\/mod_proxy.so\nLoadModule proxy_http_module modules\/mod_proxy_http.so\n\n<VirtualHost 173.255.224.150:80>\n      ServerAdmin gary.sieling@gmail.com\n      ServerName garysieling.com\n      ServerAlias www.garysieling.com\n      DocumentRoot \/srv\/www\/garysieling.com\/public_html\/\n      ErrorLog \/var\/log\/apache2\/garysieling-error.log\n      CustomLog \/var\/log\/apache2\/garysieling-access.log combined\n\n      ProxyRequests Off\n      ProxyErrorOverride Off\n\n      <Location \"\/search\">\n        ProxyPass \"http:\/\/127.0.0.1:3000\/search\"\n      <\/Location>\n\n      ProxyPassReverse \"\/search\" \"http:\/\/localhost:3000\/search\"\n<\/VirtualHost>\n<\/pre>\n<p>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).<\/p>\n<p>You may need to enable the proxy modules:<\/p>\n<pre lang=\"bash\">\na2enmod proxy\na2enmod proxy_http\n<\/pre>\n<p>I saw some documentation which recommended a hard stop of apache, to get these to really take (not sure how necessary this is):<\/p>\n<pre lang=\"bash\">\n\/etc\/init.d\/apache2 stop && \/etc\/init.d\/apache2 start\n<\/pre>\n<p>You can use &#8220;Forever&#8221; to keep a node application running:<\/p>\n<pre lang=\"javascript\">\nnpm install forever --save\nnode_modules\/forever\/bin\/forever start \\\n -append \\\n --spinSleepTime 100 \\\n --minUptime 200 \\\n -l \/var\/log\/ssl-search-forever.log \\\n -o \/var\/log\/ssl-search-output.log \\\n -e \/var\/log\/ssl-search-error.log \\\n index.js\n\nnode_modules\/forever\/bin\/forever list\n<\/pre>\n<p>There are a few caveats with logging &#8211; to get the right access log format, you need something like this:<\/p>\n<pre lang=\"javascript\">\napp.use(require(\"morgan\")(\"combined\"));\n<\/pre>\n<p>There are a couple ways to handle log rotation &#8211; it looks like you can use features built into morgan, or you can use general purpose logrotate.d which is what I did:<\/p>\n<pre>\n\/var\/log\/ssl-search*.log {\n  daily\n  dateext\n  missingok\n  rotate 7\n  compress\n  delaycompress\n  notifempty\n  copytruncate\n}\n<\/pre>\n<p><b>CDN<\/b><br \/>\nIf you&#8217;re using a CDN (e.g. Cloudflare) there are a few things to note as well:<\/p>\n<p>When you&#8217;re working on this, you&#8217;ll need to remember to disable caching.<\/p>\n<p>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.<\/p>\n<p>Cloudflare has an interesting set of caching options:<br \/>\n<img alt='' class='alignnone size-full wp-image-3835' src='http:\/\/172.104.26.128\/wp-content\/uploads\/2016\/04\/img_57162851b55e6.png' \/><\/p>\n<p>If you want it to cache the most aggressively, you can have it cache every page. Depending on what you&#8217;re trying to do, you may want to design the application to avoid query strings (?x=12345) for different pages, to make this simpler.<\/p>\n<p><b>SEO<\/b><br \/>\nYou may also want to make a <a href=\"https:\/\/www.garysieling.com\/blog\/creating-sitemap-express-js-solr-database\">sitemap in your node app<\/a> and register it with Google Webmaster Tools \/ Bing Webmaster Tools.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>All the things I had to do to get my node app running<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[12],"tags":[49,236,302,345,387,388,389,592],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3817"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=3817"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3817\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=3817"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=3817"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=3817"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}