What I Learned Using Node.js

Ringga Gustavino
5 min readJan 18, 2020

--

Photo by Caspar Camille Rubin on Unsplash

My Background

Javascript is not the first programming language I learned and I only used it a few times just to try (even though I created nothing using it). My first programming language is Java and I never get to create a real project using Javascript in my college years. So that’s my background using Javascript

Fast forward to my first job after graduating college, I was accepted as a backend developer using Java as their main programming language. So I did not see myself ever code using Javascript because I thought that I got a job as a backend Java developer , I will spend most of my career using Java. But that’s not the case

One day, my friend offered me a project to create a mentoring system using Node.js as the backend. I thought cool, maybe I can learn new stuff using Node.js. Yup I did learn a lot of new cool stuff I never knew before and a little bit frustrating ones too. I will share what I learned using Javascript for the first time.

Cool, NPM!

NPM is short for Node Package Manager. It allows you to install packages or libraries that other people created for a specific reason and use it on your project. For example you wanted to create a random number generator, one way to do it is to create a function yourself in a file or class and just use it. OR you can just use NPM to fetch someone else's package, import it, and just use it and it’s very easy to do. Just put this in your CLI inside your project

npm install number-generator

And BAM! your new library is installed and you can import it to your project anywhere you like

So NPM is like Java’s Maven or PHP’s Composer but at that time, I never use them (even though have heard it a couple of times) and just copy and paste libraries into my project. Knowing that there is this kind of stuff is really an eye opener for me, because then I know that you can install packages to your project easily.

Typescript??

So, as I worked on my project, I discovered that there is this thing called Typescript. Okay then so I thought that this has nothing to do with Javascript, but I was wrong. So Typescript is the child of Javascript in my opinion because it is created from Javascript. Typescript is a typed superset of Javascript.

So, I am used to programming languages that needs to declare its variables using types such as Java, C, Pascal, etc. I’m also aware that there are programming languages that do not need it such as Javascript, Python, etc. So I’m fully prepared about this when entering Javascript environment. What I don’t know is that there are many superset of Javascript out there and the most popular one right now is Typescript.

So at first I was writing Javascript on my Node.js project but then along the way, people kept mentioning Typescript on many different forums and documentations. I thought what is this Typescript? It turns out that Typescript is the more tidy version of Javascript. When you declare a variable, you don’t just use the key word “let” or “var” or “const” but you also need to tell the type of the variable. Is it a String, Integer, Boolean, etc. This is why they called it Typescript.

While Node.js is not fully ES6 capable, Typescript uses ES6 and that does not compile to Node.js. So in order to compile it is to convert it to ES5 using packages such as Babel. And then the converted file can be read by Node.js. Something I haven’t seen when programming Java or C++.

PROMISES AND CALLBACK HELL

Last but not least, the most frustrating thing for me is this two features. PROMISES and CALLBACKS. Most if not all of the programming languages I tried were synchronise, which means the functions, the syntax, and everything runs step by step. Except if we want it to run asynchronously we use threads and it is a whole other process. Javascript on the other hand utilise asynchronous with promise and callbacks to quickly return anything that is finish processing.

It made sense in the case of frontend development. You want a non blocking interface which if one process is not yet done, just run other processes in parallel and show the result to the user which ever one finish processing first. But in backend development while it can still be use, it’s not a very good thing and very frustrating in my opinion.

For instance you want to check if a user existed in the database or not and update their name if exist. In this case you need to run 2 queries, the select query and then the update query. Yes there are maybe other ways to do this with a single query only, but this is just an example. Here is a code snippet I would make if I did not know about callbacks and promises.

Promise example

I would expect it to log the updated result but it resulted in No Data. Why? Because of promises (Those functions that has .then() chain to it). User.findOne() and User.update() are promises, When you call promises the next code after that that is the if statement will not wait for the promise to finish and thus the variable userData will still have the value of null event though the findResult returns the user data.

So how do we make it synchronise? Well, we can put the User.update() promise inside .then() of User.findOne() or… we can use async await (more on that in another article). Callbacks are the first version of promises. They have the same functionality but just in a different format and you cannot use async await on a callback.

Callback Example

Okay, so if we choose the first method that is implementing the callback or promise inside of another callback or promise it seems that it is the best practice to do right? Well it is until you meet the famous CALLBACK/PROMISE HELL

CALLBACK HELL

Callback inside callback inside callback until you created a pyramid of callbacks. This is what I learned the hard way when using Node.js I kept on thinking for days on how I can remove this thing from my code but could not find the answer. So I stopped using callbacks and just use promises instead. Because I can use async await for promises. If I stumble upon a callback I will turn it into a promise first and then use it in my code if it potentially can create a callback hell.

Conclusion

Node.js is a very good tool to use if you want to create frontend or backend programs. I’m now a full time Node.js programmer even though I hate it back then. It’s a very powerful stack to use and very easy to deploy if you get used to it. Different from Java or PHP where you need to provide servers such as apache or nginx to deploy (and also install Java and PHP module separately without xampp, etc), you can run Node.js just using the node command and everything will run automatically.

It’s just when you start off, you will be frustrated with some of the syntax and programming flow of javascript but once you get used to it, you can feel why it is used in so many projects now backend and frontend (React, Vue, Angular).

--

--