Beginner

8 minute read

What is NodeJS

“Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine.”

“As an asynchronous event-driven JavaScript runtime, Node.js is designed to build scalable network applications.”

The beauty of NodeJS

NodeJS is basically a JavaScript environment for server-side scripting that is using JavaScript everywhere. The point is to create dynamic web pages that load it’s content on demand. That makes them really fast because content loads only when it’s needed. Content can also be updated on the page without the need to refresh the page.

A lot of modern web development frameworks are based od NodeJS, such as:

  • React

  • Angular

  • Vue

Asynchronous programming

As JavaScript itself, NodeJS is asynchronous in it’s core, but what does that mean.

Asynchronous programming is a way of writing parallel programs in which a unit of work is running separately from the main thread and notifies the calling thread of its status. That status can be completion, progress or failure. So basically, the program’s code isn’t running linearly, but rather when it’s being “called” upon. A section of code doesn’t have to wait for a section before it to finish just because that section came first. This decreases code blocking significantly.

Asynchronous programming is aiming to eliminate code blocking and it can be achieved in many ways. Here are some ways it can be done in JavaScript.

There are some packages that can help you deal with promises and asynchronous programming in general.

Understanding Node and NPM

NPM is a package manager for NodeJS used to download packages, manage versions, start projects and much more. NPM stands for Node Package Manager.

It’s the largest software package registry for open source code. It can be used to manage private development and it’s a preferred combination with NodeJS.

All packages are defined in package.json file that you can define with

  • npm init

command.

npm init

Now you can install other packages and they’ll automatically be added to you package.json. You’ll also notice that when you do that, you’ll have a node modules folder created that will contain all your packages.

Useful tips

You can also check your nodeJS and NPM versions easily, like this.

node version

Here is how the help is implemented in Node using NPM.

npm help

Here are some helpful NPM commands.

Dependencies

Your NodeJS project has dependencies and those are the packages that you’ve installed. But the deal is, those packages also have other packages as their dependencies. Not all of them, but some do, and that packages may also have their dependencies.

So the problem arises when your app is a few years old and some packages become deprecated and you have to replace them. That can become a real nightmare so it’s recommended to use as little packages as possible.

npm dependencies

You can see that our express package has a lot of dependencies and above you can see two of them. Those 2 in particular don’t have any dependencies, but some of them might.

Starting with NodeJS

It’s worth mentioning that any previous knowledge of JavaScript is beneficial. If you don’t have any previous experience with JavaScript, you can still read and understand this article, but it’s advised to learn JavaScript basics first.

Installing NodeJS

You can download NodeJS from the official site. If you’re running a Linux system, you can also download it through your terminal, like this.

Creating a HTTP server with Node

Here’s the simplest way to create a HTTP server and start it. It requires only a few lines of code.

Copy to Clipboard

And when you go to that address.

Node server

Working with Databases

Node is designed work with many databases, both relational and NoSQL but NoSQL databases are best suited with Node. That being said, let’s look at some databases that can be used with node.

Relational databases

  • PostgreSQL

  • MySQL and MySQL Server

  • Oracle

  • SQLite

NoSQL databases

  • MongoDB

  • CouchDB

  • Cassandra

  • Neo4j

and more.

You should know what type of data you’re gonna store and what kind of queries you’ll need in order to choose the best database for you. If your use case isn’t really specific, it might be wise to use a database you’re familiar with. On the other hand, this can be a nice opportunity and environment to try and learn new databases.

How to start using a database in Node

First, you need to install drivers for the database you’re gonna use. You install them with NPM as usual:

Copy to Clipboard

After that you need to create a connection with the database and you’re all set. There are a lot of packages that can help you work with database, like Express and Mongoose.

Conclusion

There’s a lot more to say, but this article is meant to be just an introduction to NodeJS, so we won’t get into much detail here.

NodeJS has much to offer and we hope that this introduction was helpful. NodeJS is mostly used for web app development. Besides that, Node can be used in ChatBot development, server-side scripting, front-end and much more. We will explore some of these in some other article.