Sitemap
Sabina S Ohlsen

Sabina S Ohlsen

student @operationspark and prospective software engineer

Introduction to node.js

3 min readAug 31, 2020

--

Not too long ago, JavaScript code could only be executed on the browser. As Javascript evolved through the years and became a more powerful language, it was still only used as a front end language, and the other languages had to be used for the back end side. But thank to Node.js which was invented in 2009, by Ryan Dahl; it quickly received a wide range of interest and it is currently still popular. Node.js is used by top companies like: Netflix, Uber or eBay.

Node.js an asynchronous event-driven JavaScript runtime. But what does this exactly mean? JavaScript and Node.js run on the V8 Javascript runtime engine. It converts code as it is run. It forms high-level code and compiles to faster machine process where computer can execute without interpreting.

What is Node REPL

Abbreviation stands for ReadEvalPrintLoop, it means that Node.js is a program that cycles between different states. REPL comes in the package when Node.js is downloaded.

Read: as name indicated it a stated where it reads the input.

Eval: where the input is evaluated.

Print: evaluation is printed.

Get Sabina S Ohlsen’s stories in your inbox

Join Medium for free to get updates from this writer.

Loop: process of looping.

Running a Program with Node

Instead of using console.log in a browser you can run JavaScript on computer, as below example shows, you can type in your terminal node and name of the file you need, in this case is App.js and the outcome will be Hello World.

Modularity

Is a technique where diffrent parts of a program are in their individual files instead of in one huge file where code may become confusing, and when they come together they create one cohesive unit. All those parts are combined together using require() function.

In above example we will take a look at two files week.js and app.js. In week.js we are adding Week class a value of module exports. If you wonder what that is, I will tell you it is an object that exists in every Node environment. What this object does is that it holds the content of that file, and it can be required to a diffrent file. So, if you look on line thirteen, in app.js, we have assigned variable Week to the module.exports object of week.js file by invoking the require(). In function, we pass the path to the module; in this case it is (‘./week.js’).

Press enter or click to view image in full size

Node Package Manager

NPM organizes modules so they can be easy found by Node, which also manages dependencies. NPM itself consists of three components: website, registry and CLI. You can locally install your npm, by running the below command. One popular package is nodemon. It is very useful tool the watches all the files in the project you working on and resets every time you make changes in the file.

When npm is installed you will have node_modules and package.json in your folder. Below is an example how package.json looks like.

Event-Driven Architecture

We often hear that Node is event driven. What does this exactly mean? Node facilitates EventEmitter module that communicates between objects in Node. We can access events using requiring.

Press enter or click to view image in full size

As you can see, it seems that Node.js is one very powerful tool with a very rich ecosystem, ready to use and to create. And can be used to build many different things like:

SPAs, single page apps, due to its asynchronous data flow and event loop it captures simultaneously user’s requests and updates.

RTAs, real-time app, like Google Doc, Slack are also asynchronies event-driven which play huge roles.

Node.js is a very powerful tool and it is not going anywhere, considering it is still very popular.

Thank you for reading.

--

--

Sabina S Ohlsen
Sabina S Ohlsen

Written by Sabina S Ohlsen

student @operationspark and prospective software engineer