NodeJS Learning Part 1

Swapnil Watkar
3 min readMar 9, 2022

--

Node.js is an open-source, cross-platform, back-end JavaScript runtime environment that runs on the V8 JavaScript engine (by google) and executes JavaScript code outside a web browser.Node.js is primarily used for non-blocking, event-driven servers, due to its single-threaded nature. It’s used for traditional web sites and back-end API services, but was designed with real-time, push-based architectures in mind.

Comparison

Traditional client-server logic :
1 request from client → server create one thread →Server ,so if 1000 req 1000 thread on server (thread need to wait for server to reply)
disadvantage: thread, waiting

NodeJs logic:

use non-blocking, event-driven I/O to remain lightweight and efficient in the face of data-intensive real-time applications that run across distributed devices.
multiple request →base on event one single thread → server

request →
request → → single thread →server
request→

question of sharing a single thread between all clients requests? yes,
but we have to code it carefully to avoid blocking.

Nodejs comes with Node Package Manager that has verious tools like Express.js,connect,socket.io etc.

Examples of Where Node.js :Chat APPLICATIONS , DASHBOARD APPLICATIONS ,MONITORING APPLICATIONS ,SERVER-SIDE WEB APPLICATIONS etc

JSON:JavaScript Object Notation
It is commonly used for transmitting data in web applications

Part 1: Nodejs

download : https://nodejs.org/en/download/
LTS version
After installation we need to run some NPM commands.
we use visual studio for runing command.
press CTRL+J for terminal in VS
run npm version after installation.

Part 2: Postman
It is a tool for testing the API.
download: https://www.postman.com/downloads/
go to google and type jsonplaceholder
https://jsonplaceholder.typicode.com/ — some fake api to testing

go to postman and run get request https://jsonplaceholder.typicode.com/todos/1

Part 3: Understand Restful API
Diff between Restful API and Front-end route?

JSON data
UI

we perform crud operation using HTTP for Restful API.
we use HTTP methods like GET ,POST,PUT and DELETE for CRUD operations.

Part 4: NodeJS Application

Create directory called NodeJs_course
Create directory called backend in NodeJs_course folder
goto backend folder using VS, open command prompt

run command npm init
package name:nodecourse
version press enter
description : backend server for all API Calls
entry point app.js
press enter for others

Package.json is created
install nodemon (npm install nodemon) for monitoring our application.
create a file in VS under Backend call app.js
add console.log(‘First Nodejs’); in app.js

run the application using
npm start on command prompt in VS

see output on console as First Nodejs

Now open package.json and edit script
add “start”:”nodemon app.js”, above the “test”

now any change to app.js will not required to reatart app,it will auto detect.

Part 5:Back-end server/Web server with Express

First thing install express
npm install express (node library for hosting)
we will start codeing for web server in file app.js
open app.js and write
const express = require(‘express’); → import express from node
const app = express(); → use constant app to use express.

we host our server on 3000 port and a call back ,which will tell us if server is running properly
app.listen(3000, ()=>{
console.log(‘server is running on http://localhost:3000');
})

Now open browser and paste http://localhost:3000
Cannot Get ? because we have not specify initial route for application.

we will provide the route now
edit app.js

add code above the listen method:

app.get(‘/’,(req,res)=>{
res.send(‘happy learning’) →call back to say message
})

open browser and paste http://localhost:3000
happy learning

Code:

const express = require(‘express’);
const app = express();

app.get(‘/’,(req,res)=>{
res.send(‘happy learning’) — →call back to say message
})

app.listen(3000, ()=>{
console.log(‘server is running http://localhost:3000');
})

We will learn how to develop API using HTTP methods in next Part (2).

Thanks.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Swapnil Watkar
Swapnil Watkar

Written by Swapnil Watkar

Hi ! I am a Software Consultant , experienced in Mobile and Web applications Also AWS Certified Solutions Architect — Associate

No responses yet

Write a response