Node.js Definition

So, let's start with the non-coding introduction to the Node.js. So first of all, why use server-side JavaScript? Because Node.js by some of the definition it's called a server-side JavaScript. So Ryan Dahl, the creator of Node.js said, "Node was originally born out of this problem, how can you handle two things at the same time?"

And JavaScript wasn't the first language that he used to experiment with the Node.js platform. First he tried it with TMT++, and then with Ruby. He settled on JavaScript because it's more expressive and then has this V8 thing in the event loop. So, what are some of the advantages of this Node.js? It's a non-blocking I/O, which means it's varied performant. And I will explain to you in more details what a non-blocking I/O means. I/O is input output. It's very, very fast because of this Google Chrome V8 engine that powers every Chrome browser.

So basically, Ryan Dahl, he took that V8 engine and he added some interface to work with file systems, he had some C++ modules. So, I will also explain how those technologies work together in Node.js. And then, my most favorite advantage of the server-side JavaScript is that you can use one language across the entire stack. You would see it over and over but basically, when you work and think in one language and your code and your logic in one language, that means you can share your files between the different layers like a browser or server-side or the database.

You can reuse the same utilities, same libraries like template engines. You can reuse the same templates as well, and utilities like Underscore or Lodash, Superagent or Request. They are very easily either ported to the browser or from the browser to the Node.js. And my personal experience is that I'm thinking 10 times faster when I have just one language. It's faster to learn just one language. You make less mistakes because of the syntax differences. So, having one context and having no context which is very, very, very beneficial for developers.

And JavaScript is a very expressive language, it's very easy to start coding in it. Someone joked that JavaScript is probably the only language that developers don't bother learning, and that's true. How often do you encounter some issue in the browser? Or how often one of your colleagues would encounter some issue in the browser and instead of reading a manual or a book on JavaScript, they would just Google that or search it on Stack Overflow, and they would copy and paste that example, snip it, try it, modify it and if it works they would leave it, right? So.

And then we have the solid standard. It's called ECMA Script, ECMA standard, that evolves JavaScript. So, basically, it's a good thing compared to languages like PHP where it was very, very, very wild. And then, it became inconsistent, for example, the naming in PHP could be very different. Okay. So, server-side JavaScript, Node.js, this is just a reiteration of the previous slide, but one huge, huge, huge thing that we have in Node.js is called npm. It's a native Node.js package manager, but the interesting thing that has happened through the past couple years is that from this open source project, npm became -and the command line tool npm- became a real company like Good Hub.

Now you can buy their enterprise version and also they provide support for you. And it's also became this huge platform for not just Node.js modules but for pretty much any JavaScript modules and packages. You can find React. You can find Angular, Backbone, other front-end modules and packages, and libraries on npm. You can find even maybe non-JavaScript modules as well. I don't have a good example right now. And then obviously you can re-use that code very, very easily with Browserify or Webpack. You can review them. You can use the browser files on the server and server files on the browser.

So, what is a non-blocking I/O or input output? Historically, input and output they are the most expensive operations in your projects, in your applications in your system. So, this is kind of a big deal. In Node.js, we have this non-blocking I/O, which is implemented by the use of the event loop. So, it's a single thread. Why it's a single thread? Because with a single thread we eliminate a lot of complexity like deadlocks and racing conditions.

And we have this event loop, think about it as a "while" or "for" loop that constantly looks for something to execute. And then all the input and output operations they performed on the system level, you could read from the file, write to a file database some expensive computations like hashes and cryptography. And when those operations are ready, when they're done, you get the callback that goes back to event loop, and that's how Node.js can process multiple requests at the same time without having to block your thread. Of course, there are some disadvantages to Node.js as well. So developers, they have to shift their thinking, because asynchronous, it's not natural. It's not how historically most of the developers and software engineers and programmers have learned to program, so it's a little bit functional and you have this prototypal concept as well. So just think in an asynchronous way.

That's usually a little bit of an obstacle for people new to JavaScript and Node.js. And then the frameworks are not as mature as the frameworks in Ruby, Java or Python yet. And this is natural because we have Node.js only for four years and we have those technologies and platforms for many many years, 10, 15, 20 years. And then JavaScript is a hot mess by itself. It has all those quirks or bad parts. But if you know the fundamentals, if you really take a couple days to read the JavaScript: The Good Parts book by Douglas Crockford or watch some videos and just really, really teach yourself on those fundamental aspects like this keyword, prototypal inheritance, functional inheritance, you will be mostly okay. And then we have ES6, we have CoffeeScript, we have TypeScript, we have ES7. So some of those problems are getting fixed and also in the Node.js itself some of those problems are fixed.

Okay. So, here's a gotcha about Node.js and that very, very amazing thing which is called non-blocking I/O. Non-blocking I/O is probably the most advantageous benefit of all other benefits. So, you can still write a blocking code in Node.js. It's totally possible. So if you have some CPU-intensive tasks, a good architecture would be to either create a new Node.js process so your main Node.js process is not blocked, or you can even use another technology like Python if you have some existing systems already. So, Node.js itself would not enforce you to not use CPU-intensive tasks, not to use expensive I/O. So, you need to be careful about that and use asynchronous non-blocking I/O methods when they are available.

So back to those quirks, some of the downsides of JavaScript. The general Node.js Node.js is that we have this thing called Callback Hell. You can even go to its website. It's and use these different ways of inheriting, creating classes. So sometimes it might get confusing.

An interesting thing is that JavaScript is not even required to write and to use and to build Node.js application. It's totally, totally cool to use something like CoffeeScript. I am a huge fan of CoffeeScript. I have a course on CoffeeScript here on Udemy, check it out. And Typescript is another good alternative. It's developed by Microsoft. It has typing and ClosureScript. It's very, very functional. It's similar to Closure which converts to Java, but ClosureScript converts to JavaScript. So, all those languages, they are compiled into JavaScript so you don't run them in the browser or in Node.js but you use a converter or compiler. So you write in CoffeeScript for example and then you compile to JavaScript.

Those are big companies that use Node.js. As you can see the list has very, very prominent web property owners like eBay,, Box, Yahoo, now IBM, Netflix and Capital One are part of this. So, you might hear often this kind of concern that Node.js is a single-threaded system and that's true. Node.js, a single Node.js process is single-threaded. So that's by design because with multi-threading you might encounter some terrible, terrible complexities. And it turns out that the single-threaded, it's really good for asynchronous code and because it keeps it simple, simple enough.

However, when you want to scale that single process how do you do that? So, to do that you would use something called clusters. So you would fork another process. So you would have multiple processes and if it's a web server that you're building you can build multiple processes. You can start multiple processes and they would be listed on the same port.