Node.js

Tony Ascroft © 2018

Spring is here and soon that will mean being able to sit outside! For me that’s a good and a bad thing. Good because I love the outdoors, hiking, biking, golfing… bad because I love the outdoors, hiking , biking… you get what I mean. Spending time at the computer becomes harder for me as the weather warms up but I’ll keep at it, I hope you will too.

No big changes since my last post. I completed the Flex Zombies course and I’m still working on Front End Development course at freeCodeCamp , I’m currently in the Basic Algorithm unit. One new thing I did do was branch into Node.js this week, here’s why… A few weeks back I was following along with a JavaScript tutorial on freeCodeCamp when I got stuck on a problem so I opened up my code editor (I use Brackets) and started editing the code to come up with a solution to the problem, but I had to copy it back into freeCodeCamp to run it and that got me thinking how can I write and test my own code creations.  I don’t want to have to create an HTML document every time I want to test a new JavaScript snippet  so I searched  ‘how to test JavaScript’. The results included using your browser and using programs like JsFiddle but every page seemed the same. Instructions and opinions about JavaScript and how to test it that all made my eyes cross trying to figure out what they were talking about.

One thing I read a few times was ‘you can use Node.js’. Now I’ve heard of Node.js. I’ve even seen it in job advertisements as a requirement ‘must know Node.js’. So I decided wouldn’t it be better to learn something that I can add to my resume rather than a program that ‘simulates’ using JavaScript? Not knowing anything about Node I installed it and tried to set up a server (which you need to run your JavaScript code) on my computer. It installed fine but when it came to accessing the localhost I just got this error message “This site can’t be reached localhost refused to connect”. I tried for hours but couldn’t find a solution and every tutorial I read just skipped over the most basic parts and jumped into language and instructions I couldn’t follow. I eventually got too frustrated , set it aside and went back to reading and doing the JavaScript challenges on freeCodeCamp.

Fast forward to this past Saturday and once again I read about someone suggesting getting to know Node was not only a good way to try out your JavaScript but that you could build apps with it too. I hate it when I can’t figure something it out. So I decided to spend a few hours and learn about Node.js. Unfortunately, much like Git (see my post Git for Absolute Newbies!), most tutorials (even the ones that say they are for beginners) go into way to much detail and they all start off expecting that you already know how to set up a server and access the localhost etc…. I don’t.

So I spent the first couple hours searching for a good Node.js for beginners tutorial and eventually I found Node.js Tutorial For Absolute Beginners from Traversy Media. Finally, a step by step tutorial that begins at the beginning and takes you through Node and Mongodb (a database) one step at a time. Of course I’m no expert and I hope my understanding of Node.js for this post is correct and help’s rather than hinders other Newbies avoid my experience, but here’s a short summary about Node.js which I suggest you follow up by watching Node.js Tutorial For Absolute Beginners  and then if you’re still interested try Node.js & Express From Scratch a 12 part video tutorial series also from Traversy Media which I haven’t gotten to yet but looks like it follows in the same style as the other two videos.

Node.js Summary:
What is Node? In 1995 JavaScript was introduced as a scripting language that could add interactivity to web pages but it only worked in web pages. Then in 2009 Ryan Dahl created Node.js. Written in C, C++, and JavaScript it is built on Google Chrome’s open-source V8 JavaScript engine, that’s the thing that takes the JavaScript code and converts it to machine language so your computer can run it. What Node.js did was allow JavaScript to be used outside of web pages, meaning it could now be used for back end servers and stand alone apps.

Node is an an event-driven, non-blocking I/O model which means it doesn’t handle one request at a time and wait until the data comes back before handling the next request. Instead it takes the request and puts it aside while the computer puts together the data requested, moving on to the next request. For a better explanation watch the first 10 minutes of Node.js Tutorial for Beginners: Learn Node in 1 Hour. 

Node.js will allow you to write and test JavaScript code and it will allow you to set up a server that you can program using JavaScript. Once installed Node can be accessed and run from any cmd line program. Just type ‘node’ first then the code or better yet, create a new JavaScript file. ie. app.js in your code editor then run it from the cmd line. The Node.js Tutorial For Absolute Beginners tutorial explains it all.

I’ll end this post with one piece of very important advice. When you install Node.js or any or its add-ons like Express. First, before you do any installing, make sure you launch your cmd line app as the ADMINISTRATOR (right click on your menu and select ‘run as administrator’). The tutorials do say to do this but I didn’t take notice. Not once but about three times! I installed node, started the server and got “This site can’t be reached localhost refused to connect” each time. Deleted the whole installation and started again. Finally I noticed the cmd line app the instructor was using said Administrator right there on the app header. DOH!  lol  Well one thing about being self taught, after you spend hours trying to solve a problem, when you find the solution and its your own fault… you won’t make that mistake again! Launched the app as administrator, installed node and express, launched the server!  Success!

Oh, and if you’re installing Node.js 3.6 or later… the command REST? just ignore it… leave it out, and all will be fine. 🙂

Till next time!

Tony

Leave a Reply

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