{"id":255,"date":"2014-03-04T17:39:14","date_gmt":"2014-03-04T17:39:14","guid":{"rendered":"http:\/\/nikola-breznjak.com\/blog\/?p=255"},"modified":"2015-08-10T07:15:55","modified_gmt":"2015-08-10T07:15:55","slug":"getting-started-with-node-js-on-heroku-on-a-windows-machine","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/javascript\/nodejs\/getting-started-with-node-js-on-heroku-on-a-windows-machine\/","title":{"rendered":"Getting Started with Node.js on Heroku on a Windows machine"},"content":{"rendered":"<blockquote><p><a href=\"https:\/\/www.heroku.com\">Heroku<\/a> is a cloud platform as a service supporting several programming languages which lets app developers spend their time on their application code, not managing servers, deployment, ongoing operations, or scaling&#8230;<\/p><\/blockquote>\n<p>If you tried to get your feet wet with Heroku by deploying Node.js application, you must have come across <a href=\"https:\/\/devcenter.heroku.com\/articles\/getting-started-with-nodejs\">this article<\/a>\u00a0on Heroku&#8217;s documentation (and you also may have run into problems when starting <a href=\"http:\/\/ddollar.github.io\/foreman\/\">Foreman<\/a>\u00a0if you&#8217;re on a Windows machine &#8211; read onward to see how I&#8217;ve solved it).<\/p>\n<p>Obviously, first you have to make an <a href=\"https:\/\/id.heroku.com\/signup\/devcenter\">account on Heroku<\/a>, then install <a href=\"https:\/\/toolbelt.heroku.com\/\">Heroku toolbelt <\/a>(it gives you git, foreman, and heroku command line interface) for your environment (in my case Windows):<br \/>\n<a style=\"line-height: 1.5;\" href=\"http:\/\/nikola-breznjak.com\/blog\/wp-content\/uploads\/2014\/03\/herokuToolbelt.png\" rel=\"lightbox[255]\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-258\" src=\"http:\/\/nikola-breznjak.com\/blog\/wp-content\/uploads\/2014\/03\/herokuToolbelt-300x176.png\" alt=\"herokuToolbelt\" width=\"300\" height=\"176\" srcset=\"https:\/\/nikola-breznjak.com\/blog\/wp-content\/uploads\/2014\/03\/herokuToolbelt-300x176.png 300w, https:\/\/nikola-breznjak.com\/blog\/wp-content\/uploads\/2014\/03\/herokuToolbelt.png 604w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Fire up your command prompt (I use <a href=\"http:\/\/sourceforge.net\/projects\/console\/\">Console2<\/a>) and run\u00a0<span class=\"lang:default decode:true  crayon-inline \">heroku login<\/span>\u00a0. You need to have\u00a0<a href=\"http:\/\/nodejs.org\/\">Node.js<\/a> installed (in case you don&#8217;t you can download it <a href=\"http:\/\/nodejs.org\/download\/\">here<\/a>).<\/p>\n<p>Write some Node.js app and put it in <span class=\"lang:default decode:true  crayon-inline\">app.js<\/span>\u00a0file:<\/p>\n<pre class=\"lang:default decode:true\">\/\/app.js\r\nvar express = require(\"express\");\r\nvar app = express();\r\n\r\napp.get('\/', function(req, res) {\r\n  res.send('Every day in every way I\\'m serving more requests');\r\n});\r\n\r\nvar port = Number(process.env.PORT || 5000);\r\napp.listen(port, function() {\r\n  console.log(\"Listening on \" + port);\r\n});<\/pre>\n<p>If you have \u00a0<span class=\"lang:default decode:true  crayon-inline\">package.json<\/span>\u00a0\u00a0file then Heroku will recognize your app as Node.js app. In order to create it,\u00a0run <span class=\"lang:default decode:true  crayon-inline \">npm init<\/span>\u00a0\u00a0in the root directory of your app. The npm init utility will walk you through creating a package.json file by asking few questions. If you already made your repository on GitHub and cloned it locally then npm init command in this folder will recognize that too and add:<\/p>\n<pre class=\"lang:default decode:true\">\"repository\": {\r\n    \"type\": \"git\",\r\n    \"url\": \"https:\/\/github.com\/yourUsername\/appName.git\"\r\n}<\/pre>\n<p>If you wish, you can freely clone my test app from Github:\u00a0<a href=\"https:\/\/github.com\/Hitman666\/herokuTestApp\">https:\/\/github.com\/Hitman666\/herokuTestApp.git<\/a><\/p>\n<p>Next, install dependencies from your code (in <span class=\"lang:default decode:true  crayon-inline\">app.js<\/span>\u00a0\u00a0example the required module is\u00a0<a href=\"http:\/\/expressjs.com\/\">express<\/a>). Use <span class=\"lang:default decode:true  crayon-inline \">npm install &lt;pkg&gt; &#8211;save<\/span>\u00a0\u00a0to install a package and save it as a dependency in the package.json file. In my example that would be one command:<\/p>\n<pre class=\"lang:default decode:true\">npm install express --save<\/pre>\n<p>&nbsp;<\/p>\n<p>You have to set a\u00a0<a href=\"https:\/\/devcenter.heroku.com\/articles\/procfile\">Procfile<\/a>, which is a text file in the root directory of your application, to explicitly declare what command should be executed to start a web dyno. In our case this would be the contents of Procfile:<\/p>\n<pre class=\"lang:default decode:true\">web: node app.js<\/pre>\n<p>Now you should be able to start your application locally using Foreman (installed as part of the Heroku Toolbelt) by running\u00a0<span class=\"lang:default decode:true  crayon-inline \">foreman start<\/span>\u00a0.<\/p>\n<p style=\"text-align: center;\"><span style=\"line-height: 1.5;\"><strong>But here the party started<\/strong> :\/<\/span><\/p>\n<p><span style=\"line-height: 1.5;\">In my case foreman didn&#8217;t start at all, and after a lot of searching, I managed to solve it in few steps. First, I updated my Ruby installation:<br \/>\n<\/span><\/p>\n<pre class=\"lang:default decode:true\">gem update --system --source http:\/\/rubygems.org\/<\/pre>\n<p>Then, according to <a href=\"http:\/\/stackoverflow.com\/questions\/15399637\/cant-start-foreman-in-heroku-tutorial-using-python\">this StackOverflow post<\/a> I installed former version of foreman:<\/p>\n<pre class=\"lang:default decode:true\">gem uninstall foreman\r\ngem install foreman -v 0.61<\/pre>\n<p>Finally, I\u00a0<span style=\"line-height: 1.5;\">added Ruby&#8217;s bin folder (Ruby which, in my case, is in <span class=\"lang:default decode:true  crayon-inline\">C:\\Program Files (x86)\\Heroku\\ruby-1.9.2\\bin<\/span>) to my PATH variable.<\/span><\/p>\n<p>Ok, almost there! Lets add our app to git (you should skip the git init command if you already cloned your app from GitHub):<\/p>\n<pre class=\"lang:default decode:true\">git init\r\ngit add .\r\ngit commit -m \"init\"<\/pre>\n<p>and finally, lets deploy it to Heroku:<\/p>\n<pre class=\"lang:default decode:true\">heroku create\r\ngit push heroku master<\/pre>\n<p>To\u00a0open the app in your browser run\u00a0<span class=\"lang:default decode:true  crayon-inline \">heroku open<\/span>\u00a0. To push the changes to GitHub you have to execute <span class=\"lang:default decode:true  crayon-inline  \">git push origin master<\/span>.<\/p>\n<p><span style=\"font-family: 'Source Sans Pro', Helvetica, sans-serif; font-size: 16px; line-height: 1.5;\"><strong>*<\/strong>Now that your app is up and running on heroku (<a href=\"http:\/\/gameria.herokuapp.com\/\">my link<\/a>) you may want to prevent it from going to sleep mode. Some suggestions on how to achieve this are in <a href=\"http:\/\/stackoverflow.com\/questions\/5480337\/easy-way-to-prevent-heroku-idling\">this post on StackOverflow<\/a>\u00a0(I went with <a href=\"http:\/\/uptimerobot.com\/\">UptimeRobot<\/a>).<br \/>\n<\/span><\/p>\n<p><strong>**<\/strong>At some point, as I was fiddling with the apps on Heroku&#8217;s website, I deleted all apps and then the <span class=\"lang:default decode:true  crayon-inline\">git push heroku master<\/span>\u00a0\u00a0command from my console was not working anymore, so I had to do:<\/p>\n<pre>git remote rm heroku\r\nheroku create\r\ngit push heroku master<\/pre>\n<p><strong>***<\/strong>Also, I wanted to rename my app and that can be done easily:<\/p>\n<pre class=\"lang:default decode:true\">heroku apps:rename NEWNAME<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Heroku is a cloud platform as a service supporting several programming languages which lets app developers spend their time on their application code, not managing servers, deployment, ongoing&hellip;<\/p>\n","protected":false},"author":1,"featured_media":257,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,4,14],"tags":[],"class_list":["post-255","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-heroku","category-nodejs","category-windows"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/255","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=255"}],"version-history":[{"count":15,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/255\/revisions"}],"predecessor-version":[{"id":4160,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/255\/revisions\/4160"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/257"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=255"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=255"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=255"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}