Backend with NodeJS - Part 1

Posted by Abhishek on April 01, 2020

NodeJS Deep Dive

In this short deep dive, you will learn how to create backend for your application using NodeJS.

1. Introducing Node & its Architecture

NodeJS is an open source, cross platform runtime environment for executing javascript code outside of a browser. Mostly, Node is used to build backend services (also called as API [Application Programming Interface]). API’s are the ones that power our Web Applications or Mobile Apps. The Web Applications or Mobile Apps are just the user interface aspect on which users interact. The user interface is only powerful if it can talk to a backend service and do operations like save data to database, send emails, etc.

Node is ideal choice for developing highly scalable, data intensive and real time services that power client applications.

Benefits of using Node
  1. It is a great choice for prototyping and agile development.
  2. Ideal for fast and highly scalable services. Companies like Uber, Netflix, Paypal use Node a lot.
  1. Node uses Javascript everywhere.
  1. Consistent codebase.
  1. Node has the largest ecosystem of open source libraries.
  1. Node is high scalable because of its non-blocking asynchronous nature. It works on single thread but with asynchrony, it uses that thread very efficiently.
Remember This

Node is not an ideal choice for CPU intensive applications.

Node Architecture

Initially, browsers were the only place where we could run our javascript code. Browsers have a Javascript Engine which takes the javascript code and converts it to computer understandable machine code and runs it.

JS Engine

Javascript engines on various browsers are different. That’s probably the reason why the same javascript code behaves differently in different browsers.

The javascript engine that resides inside the browser is the runtime environment for javascript code. Till the year 2009, the only way to execute javsscript was in browsers. “Ryan Dahl” (the original developer of Node) took the “v8” JS engine and embedded it in a C++ program and named it as “Node”.

Node

Understand Node and get it fundamentally RIGHT

2. Preparing the Node environment in your machine

On Windows Operating System

On Ubuntu Linux

sudo apt-get install curl
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
sudo apt-get install nodejs
node -v
npm -v

General softwares to install

Installing node in your machine will ensure that Node and NPM are both setup properly. That’s it. We are done with preparing the environment.

3. Creating the first program in Node

Note that I am on Windows machine and the steps outline Windows Terminal commands

4. Setting up Express Package

Now, you have successfully set up Express with Node and the “/” endpoint of your API is running and returning a response. Awesome :)

Ok. Let’s get a little serious and move away from the “Hello World” examples.

Let’s develop an API to manage a list of criminals [I mean ficticious criminals] with in-memory data management

const express = require('express');
const app = express();

app.use(express.json());

app.get('/', (req, res) => {
  res.send('Hello World');
});

const ficticiousCriminals = [
  { name: 'Mr.Pereira', type: 'Smuggler' },
  { name: 'Benjamin Bruno', type: 'Smuggler' },
];

app.get('/api/criminals', (req, res) => {
  res.send(ficticiousCriminals);
});

app.post('/api/criminals', (req, res) => {
  const newCriminal = {
    name: req.body.name,
    type: req.body.type,
  };
  ficticiousCriminals.push(newCriminal);
  res.send(ficticiousCriminals);
});

const port = process.env.POT || 3000;
app.listen(port, () => {
  console.log(`Listening to port ${port} ...`);
});

Get Criminals

Post Criminal

An API development this fast & easy is the power of NodeJS. I believe this deep dive was usefull for you. In the next deep dive, we will focus on moving the in-memory data to a persistant data storage.


Thanks for reading this post. Enjoy !!
Share on: