A noob’s introduction to NodeJS

What is NodeJS? How does it work? How can I start writing some code?

Bruno Lombardi
brunolombardi
4 min readApr 18, 2018

--

by Fatos Bytyqi on Unsplash

Yes, the powerful software that is used by companies like Uber, Netflix and PayPal, is simpler than you can think. That’s kind of the major idea of Node, to be able to get you started with nothing more than 10 lines of code. But, before we start writing some code, let’s understand what NodeJS is.

Let there be back-end JavaScript, then there was back-end Javascript

Probably, you are used to run JavaScript inside of your browser. The browser, which is responsible for reading, processing and sending the users’s input to the server, is the frontend of our application. This is only possible because to run JavaScript your browser is built with a JavaScript Engine. Google Chrome’s JavaScript engine is called Chrome V8. It is very robust, stable and fast. But the best part, it’s open source. So as NodeJS!

Wait, is that an infographic?

Oh, and I forgot to say it, but you can take the V8 engine out of Google Chrome. NodeJS is built on it. It extends it, and made possible for JavaScript code to be ran outside of the browser. That means you can think of NodeJS (Node, for friends) as an JavaScript environment similar to your browser. But, Node can access your file-system, connect directly to databases, and even create and manage web servers.

https://imgflip.com/i/28mc4b

But I can do that with PHP, ASP.NET and even Python, which isn’t even a real programming lan… oops

Jokes apart (I love python, but I love jokes), there is a very detailed list of pros and cons of each technology you choose for writing your web apps. But that’s not the focus of this article. So, I’ll tell you just two simple words: non-blocking I/O (that’s two words, right?).

https://imgflip.com/memetemplate/94552254/SPONGEBOB-MULTITASK

Can you cook and drive at the same time? Probably not, as this can be physically impossible. But NodeJS, it also can’t do it. Instead, it is smart enough to put the cooking task in a queue, (just like you, if you’re not that hungry) and start driving (yes, with no logical reason, cause I couldn’t think a better example). It doesn’t wait till the cooking is done to start driving, but when it’s done cooking, it is ready to eat a delicious meal.

Understanding non-blocking I/O

I know I probably gone too far with that example, but let me give you a javascript example:

In this script, we can see non-blocking I/O working in javascript. The setTimeout function is a non-blocking function, because it won’t stop the normal execution of the code. The code after it will run, that’s why you see the output in an weird order.

Now, if you noticed, setTimeout ran the function passed to it after the time you specified, which in in this case was 2000 milliseconds, or 2 seconds. Try to run this code in your browser’s JavaScript console, and you will see that everything is ran instantly, except for the function inside setTimeout, which is ran only after 2 seconds.

I know I am taking too long to actually start giving you some code to write in NodeJS. My objective here is not to teach how to install it, configure it, or run code with Node. I just want to teach you some simple things about how NodeJS works, and how it was created. But I also promised you how to get started.

Get started coding NodeJS

Google

Why do we need to hassle ourselves with installation and configuration of NodeJS? Well, we don’t. We can do it online! Visit repl.it and start writing and compiling some code. It’s so easy to get started and to work with multiple files, that I won’t teach you anything more, just get out of here and start coding!

PS: sorry for my bad english, I’m still learning. Please, if I am wrong about anything, just tell me in the comments and I’ll do my best to correct it.

--

--