Learning Node.js basics in less than 22 hours
I just finished presenting Node.js after a 22 hour tech learning sprint. (Technically less than that since I did sleep). Â It was fun to learn something new, yet rather difficult trying to cram everything into a 10 minute presentation, (and make sure the info youâre presenting is actually accurate...) Â Â Â Iâll include the CliffsNotes version here! (random: look! CliffsNotes is now camelCase! :D)
On its website Node.js, defines itself as a âJavaScript runtime built on Chrome's V8 JavaScript engine.â Â What this means is itâs javascript code on the server side running on a very fast engine. Â
It uses JavaScript. Â JavaScript on the front-end, and Javascript in the backend. Â Whatâs not to love? Â no code potential conflicts?
Also, Node.js works asynchronously just like JavaScript in the browser, which means non-blocking processes run pretty much at the same time because it doesnât wait for something to finish before starting another one. (Itâs shuffled off the call stack to a worker that will trigger any callback functions). So that meansâŚ. you guessed it! Itâs super fast! Â
One example of this is being to simultaneously read and write the same file at same time. Â Itâs not exactly the same time because one method is still started before another (as written in the order of your script), but more it doesnât have to wait for a file to finish writing before starting to read/process it and vice versa.
Node.js is also event-driven! Â If youâve used JavaScript before, youâve most likely fell in love with the event listeners that allow your users to interact with your webpage. Itâs also here in Node.js but with different events attached, since youâre not working with the browser window object anymore. Â There are even identically named methods, like setTimeout(), setInterval(), and .on(). Â Â
Itâs highly scalable due to its asynchronous nature. The non-blocking characteristic allows it take a take on an enormous number of simultaneous connections with a high processing rate.
Scalable: same reason for big file I/O; you can do many things at the same time quickly (simultaneous connections to multiple clients)
Anything you want real-time : Node.js works really well for anything you want in real-time, such as chat apps. Â
Listening for events: its event loops checks for the events youâve set listeners on and runs the attached callback methods. Â
Big File I/O: Â Since itâs non-blocking, you can access the same file at the same time, start processing incoming data before itâs finished loading, etc.
CPU intensive processes pretty much negates its speediness.
Easy to end up in callback hell. Too many callbacks to keep track of...
Real-time Accurate Database dependent data: database processes are pretty much offloaded into callback methods, and the near instantaneous information you have now is not so instantaneous/accurate now until all the database methods have finished running.
A visit to the official Node.js site lists various methods to install Node (and npm). Â I installed with homebrew ($ brew install node) and it took less than a minute.
Sidenote: npm, short for node package manager, gives you access to the npm online repo of the many node modules.  Itâs like what gems are for ruby.  Itâs extremely useful for easy installation of modules and  automatically installs all the moduleâs dependencies. Just $ npm install _fabulousModuleName
To use the REPL/console for running js in terminal, just enter node and you can run javascript code here (like what irb is to ruby). Â To exit the console, type process.exit(0) or Ctrl-C twice. $ node someFile.js to run someFile.js
console.log in your JS file will output to your terminal (stdout)
Random interesting things about Node.js
In addition to boolean, number, string, and regex, thereâs a new datatype called buffer. Â Â Node.js docs describe Buffer as â[Raw data] stored in instances of the Buffer class. ... similar to an array of integers but corresponds to a raw memory allocation outside the V8 heap.â Â Itâs basically a chunk of memory. Â
The Node.js Documentation is also color-coded for stability of a specific feature, which is interesting. Â Although Iâd like it even more if there were examples for all methods instead of just a few methods.
Everything in Node.js that has events are instances of the class event.EventEmitter .
Thereâs also this interesting thing call a stream, which is not a datatype. Â It seems to correspond to a âstreamâ of data like files, input, and output.
Node.js code vs. javascript on the front-end Â
There are some differences: no access to the browser window object, since youâre on the backend, but syntax is still JS syntax. Â
One key difference I should mention is how to include your .js files into your code. Â On the front-end, you include the link to your javascript file in your html code. Â But there isnât an html in the back-end! Â In Node.js, any modules are required at the top of the file (using the CommonJS module pattern) like this:Â var http = require('http');
A Node.js web server is pretty easy to implement. I included the bare bones of one below that I bound to port 3005 of my computer and served my index.html file when I visited localhost:3005 in my browser. You can easily find many other different examples.
var http = require('http'); var fs = require('fs'); http.createServer(function(request, response){ //can(/should) include error handling with error status codes, etc. response.writeHead(200, {"Content-Type": "text/html"}); fs.readFile('index.html', function(error, contents){ if (error) console.log(error); // something to see errors // can write multiple globs of data with a bunch of response.write() response.write(contents); response.end(); }); }).listen(3005);
Note: you technically can put the content of your response as an argument in response.end() without having to use response.write(), like: response.end(âThis is a cool response!â) Â Â
This is not a web framework though. Â So, you would have to hand roll one yourself or use one of the many wonderful frameworks built on Node.js, such as Meteor, which is already on my giant list of things to eventually check out.
Iâm have to see how to include my browser JS file with my index.html file when serving with my Node webserver.  It seems to be overwriting it with my html file.  hmm⌠more learning and research!
My classmatesâ topics ranged from D3 to React to Redis to Ionic. Â Â Joseph even did a presentation on on socket.io, which is built on Node.js, and used made a basic chat app demo! Â
So many things to learn, so little time. Â Itâs more a lifelong journey, than a sprint. Â Enjoy the journey! Â (...with lots of level up opportunities! Â Game on.)
TIP: When researching be aware of when the information youâre looking for was written. Â The info might be outdated/deprecated/rendered obsolete with newer versions/fixes, or simple third-party add-ons.