Posted by Abhishek on April 01, 2020
In this short deep dive, you will learn how to create backend for your application using NodeJS.
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.
Node is not an ideal choice for CPU intensive applications.
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.
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”.
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.
Note that I am on Windows machine and the steps outline Windows Terminal commands
cd D:\NodeProjects
mkdir
command to create a directory for your node project
mkdir my-first-node-project
cd
into the newly created folder
cd .\my-first-node-project\
npm init --yes
and press Enter. This will create a package.json and initialize your project.
npm init --yes
code .
and press Enter. This will open Visual Studio Code and load the current node project folder.
code .
console.log('Hello World');
Ctrl + `
. This will open the Terminal inside Visual Studio Codenode app.js
npm i express
Delete the console.log('Hello World');
line and type the following code and save.
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello World');
});
const port = process.env.POT || 3000;
app.listen(port, () => {
console.log(`Listening to port ${port} ...`);
});
node app.js
Now, open the browser and type “http://localhost:3000/” and press Enter. You should see “Hello World” display in the browser page.
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} ...`);
});
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.