{"id":416,"date":"2018-06-23T15:28:55","date_gmt":"2018-06-23T19:28:55","guid":{"rendered":"https:\/\/www.unliterate.net\/?p=416"},"modified":"2018-06-23T15:30:13","modified_gmt":"2018-06-23T19:30:13","slug":"my-day-with-node-js","status":"publish","type":"post","link":"https:\/\/www.unliterate.net\/index.php\/2018\/06\/23\/my-day-with-node-js\/","title":{"rendered":"My day with node.js"},"content":{"rendered":"<p>It&#8217;s been a long time since I had <strong>FUN<\/strong> with a programming language. Today I had decided to take some time and dig nose deep into <a href=\"https:\/\/nodejs.org\/en\/\" target=\"_blank\" rel=\"noopener\">node.js<\/a>. I&#8217;ve written much with Javascript before, and have always wanted to create RESTful services with something that wasn&#8217;t PHP, and <em>from what i&#8217;ve heard<\/em> node.js is a nice transition for a developer wanting to learn a new thing.<\/p>\n<p>So, step 1 was to get some revision loaded up. Instead of apt-get or yum installing myself a version, I decided to get v8.11.3 (at this time is LTS) and install it in \/usr\/share\/node. I symlinked all necessary executables to \/usr\/bin, and node -v&#8217;d myself to satisfactory execution.<\/p>\n<p>I did the obligatory <a href=\"https:\/\/github.com\/mjheick\/nodejs-learnings\/blob\/master\/001-hello-world.js\" rel=\"noopener\" target=\"_blank\">hello-world.js<\/a>, and a subsequent cURL later to localhost:3000 got me really tickled. Being familiar with Javascript this was a confusing walk in the park. I decided to go ahead and github\/public my musings and ramblings and set some goals, as I do have a destination with this.<\/p>\n<p><a href=\"https:\/\/github.com\/mjheick\/nodejs-learnings\" rel=\"noopener\" target=\"_blank\">https:\/\/github.com\/mjheick\/nodejs-learnings<\/a><\/p>\n<p><a href=\"https:\/\/github.com\/mjheick\/nodejs-learnings\/blob\/master\/002-is-global.js\" rel=\"noopener\" target=\"_blank\">002-is-global.js<\/a>: First thing was to find out if I have access to global variables. Yes. Yes I do. Thanks Javascript for still allowing this to happen.<\/p>\n<p><a href=\"https:\/\/github.com\/mjheick\/nodejs-learnings\/blob\/master\/003-what-is-req-and-res.js\" rel=\"noopener\" target=\"_blank\">003-what-is-req-and-res.js<\/a>: Then, with this res and req, I had to find out the contents of them. There had to be a Data::Dumper or var_dump somehow, and (since Javascript) there was the trusty console.log() to inspect these variables. #awesome<\/p>\n<p><a href=\"https:\/\/github.com\/mjheick\/nodejs-learnings\/blob\/master\/004-rest-verbs.js\" rel=\"noopener\" target=\"_blank\">004-rest-verbs.js<\/a>: Cause we&#8217;re building something RESTful, I needed to think if I had access to some Verbs. Low and behold, after skimming the <a href=\"https:\/\/nodejs.org\/api\/http.html\" rel=\"noopener\" target=\"_blank\">node.js api for http<\/a> the .method was smack in my face. It really came down to RTFM.<\/p>\n<p><a href=\"https:\/\/github.com\/mjheick\/nodejs-learnings\/blob\/master\/005-list-add-remove.js\" rel=\"noopener\" target=\"_blank\">005-list-add-remove.js<\/a>: I just took off from there. We have access to a global, we can make logic decision based on methods passed, and now we can pass a querystring and parse it up! At this point things were looking fairly awesome.<\/p>\n<p><a href=\"https:\/\/github.com\/mjheick\/nodejs-learnings\/blob\/master\/006-post-list-add-remove.js\" rel=\"noopener\" target=\"_blank\">006-post-list-add-remove.js<\/a>: We can&#8217;t use querystring for long, so I had to convert it to data passed in the request. After reading how <a href=\"https:\/\/nodejs.org\/api\/events.html\" rel=\"noopener\" target=\"_blank\">Events and EventEmitter<\/a> worked, this seemed quite natural. Got into some try\/catching as well, and now explicitly taking in JSON data as well. Just a couple steps away from what we need to actually do!<\/p>\n<p><a href=\"https:\/\/github.com\/mjheick\/nodejs-learnings\/blob\/master\/007-list-with-expired-items.js\" rel=\"noopener\" target=\"_blank\">007-list-with-expired-items.js<\/a>: At this point we needed to store data with some &#8220;life&#8221; tied to it. i had to kick in some setInterval logic to scan the list of items that we&#8217;re storing and cull any that have expired. I also approached a level of pride in my code by making it look more presentable. At this point of excitement I had decided to develop <a href=\"https:\/\/github.com\/mjheick\/nodejs-learnings\/blob\/master\/007-dataloader.php\" rel=\"noopener\" target=\"_blank\">a dataloader<\/a> to &#8220;test&#8221; how well it can load and purge data, as well as overall node.js performance.<\/p>\n<p><a href=\"https:\/\/github.com\/mjheick\/nodejs-learnings\/blob\/master\/008-persistent-executed-list.js\" rel=\"noopener\" target=\"_blank\">008-persistent-executed-list.js<\/a>: In loading node.js and terminating it I had to think ahead in &#8220;what to do if the process bombs out with data stored in it&#8221;. At this point I had plugged in two separate items:<br \/>\nA) process.on(), to hook in synchronous routines that saved data and performed any cleanup, and rightfully aborted the process.<br \/>\nB) fs.readFile() fs.writeFile, so that I can dump the &#8220;local memory storage&#8221; timely to disk in 30 seconds interval. <\/p>\n<p>Overall, this was awesome. I wanted to be able to create a RESTful service quick, timely, and efficiently, with little &#8220;new knowledge&#8221; required from previous Javascript development, and this was exactly what I got.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s been a long time since I had FUN with a programming language. Today I had decided to take some time and dig nose deep into node.js. I&#8217;ve written much with Javascript before, and have always wanted to create RESTful services with something that wasn&#8217;t PHP, and from what i&#8217;ve heard node.js is a nice [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-416","post","type-post","status-publish","format-standard","hentry","category-geek-instructions"],"_links":{"self":[{"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/posts\/416","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/comments?post=416"}],"version-history":[{"count":7,"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/posts\/416\/revisions"}],"predecessor-version":[{"id":423,"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/posts\/416\/revisions\/423"}],"wp:attachment":[{"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/media?parent=416"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/categories?post=416"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/tags?post=416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}