Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

NodeJS Master - Notes

This document provides an introduction to Node.js, covering its architecture, features, and components. It explains the differences between frontend, backend, and database technologies, and details how Node.js operates in a non-blocking, asynchronous manner. Additionally, it includes practical examples of using Node.js modules, creating servers, handling files, and managing events.

Uploaded by

reyance299
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

NodeJS Master - Notes

This document provides an introduction to Node.js, covering its architecture, features, and components. It explains the differences between frontend, backend, and database technologies, and details how Node.js operates in a non-blocking, asynchronous manner. Additionally, it includes practical examples of using Node.js modules, creating servers, handling files, and managing events.

Uploaded by

reyance299
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 168

With Node.

JS

Instructor – Satya Priya Arya


Introduction to Node.js
Frontend v/s Backend v/s Database
 The front end is the part of the website users can see
and interact with such as the graphical user interface
(GUI) and the command line including the design,
navigating menus, texts, images, videos, etc.
Examples – HTML, CSS, Java-Script, jQuery, React
 The backend, on the contrary, is the part of the website
users cannot see and interact with.
Example – C, C++, Java, Node.JS, C#
 The database used to store permanent information
Example – SQL Server, MySQL, MongoDB
What is Node.JS
 Node.js is an open source server environment
 Node.js runs on various platforms (Windows, Linux,
Unix, Mac OS X, etc.)
 Node.js uses JavaScript on the server
History of Node.js
 Before 2009, web browsers used JavaScript.
 JS was invented in the 1990 as a scripting language for
adding dynamic features to a web page from an HTML
file.
 On the other hand, Ryan Dahl created a runtime
environment for JavaScript to operate outside of web
browsers in 2009.
 npm, a package manager for the Node.js environment,
was released in January 2010.
 Current version is 19.4.0 (LTS version is 18.13.0)
Why Node.js – part 1/2

 Typical backend application works


 Receive Request
 Process Request
 Send Response
 Wait for next request
 How Node.JS work
 Receive Request
 Ready to handle next request
 Send response when previous request completed
Why Node.js – part 2/2

 Node.js eliminates the waiting, and simply continues


with the next request.
 Node.js is
 single-threaded,
 non-blocking,
 asynchronous programming
Node.js Architecture – part 1/3

 Requests: Depending on the actions that a user needs to perform, the


requests to the server can be either blocking (complex) or non-
blocking (simple).
 Node.js Server: The Node.js server accepts user requests, processes
them, and returns results to the users.
 Event Queue: The main use of Event Queue is to store the incoming
client requests and pass them sequentially to the Event Loop.
 Thread Pool: The Thread pool in a Node.js server contains the threads
that are available for performing operations required to process
requests.
 Event Loop: Event Loop receives requests from the Event Queue and
sends out the responses to the clients.
 External Resources: In order to handle blocking client requests,
external resources are used. They can be of any type ( computation,
storage, etc).
Node.js Architecture – part 2/3
Node.js Architecture – part 3/3

 Users send requests (blocking or non-blocking) to the server for


performing operations.
 The requests enter the Event Queue first at the server-side.
 The Event queue passes the requests sequentially to the event
loop. The event loop checks the nature of the request (blocking
or non-blocking).
 Event Loop processes the non-blocking requests which do not
require external resources and returns the responses to the
corresponding clients
 For blocking requests, a single thread is assigned to the process
for completing the task by using external resources.
 After the completion of the operation, the request is redirected
to the Event Loop which delivers the response back to the client.
Working and Features
Installation and Setup
 Install Node.JS from
https://nodejs.org/
 Install Visual Studio Code from
https://code.visualstudio.com/download
REPL Environment
 REPL is a computer environment like a Windows
console or Unix/Linux shell where a command is
entered and the system responds with an output in an
interactive mode. Node.js or Node comes bundled
with a REPL environment
 Read − Reads user's input, parses the input into
JavaScript data-structure, and stores in memory.
 Eval − Takes and evaluates the data structure.
 Print − Prints the result.
 Loop − Loops the above command until the user
presses ctrl-c twice.
REPL Environment Samples
 $ node
>1+2
3
 $ node
> console.log("Hello World")
Hello World
 $ node
> var x = 0
undefined
> do {
... x++;
... console.log("x: " + x);
... }
while ( x < 5 );
x: 1
x: 2
x: 3
x: 4
x: 5 undefined
>
REPL Commands
 ctrl + c − terminate the current command.
 ctrl + c twice − terminate the Node REPL.
 ctrl + d − terminate the Node REPL.
 Up/Down Keys − see command history and modify previous
commands.
 tab Keys − list of current commands.
 .help − list of all commands.
 .break − exit from multiline expression.
 .clear − exit from multiline expression.
 .save filename − save the current Node REPL session to a file.
 .load filename − load file content in current Node REPL
session.
Components of Node.js
 Node contains many components to develop, test and
deploy applications. Main components are
 Node CLI
 NPM
 Package.json
 Modules
 Third party modules (example mongodb)
 Core modules (example update)
 Development tools and framework
Node.js Modules
Modules
 Modules is same as libraries.
 functionality you want to include in your application.
 Module in Node.js is a simple or complex functionality
organized in single or multiple JavaScript files which
can be reused throughout the Node.js application
 Few build-in modules are fs, http, net, os, path,
querystring, stream, url, util
 We can create our own modules too.
First Program
 var http = require('http');

http.createServer(function (req, res) {


res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello World!');
}).listen(8080);
 Save file as hello.js
 Run “node hello.js” from terminal
 Open browser and open http://localhost:8080/
 Modify res.end('Hello World!'); to res.write(req.url)
and try with http://localhost:8080/Hello/
Module Exports
 The module.exports in Node.js is used to export any
literal, function or object as a module. It is used to
include JavaScript file into node.js applications.
 module.exports = literal | function | object
Here the assigned value (literal | function | object) is
directly exposed as a module and can be used directly.
 module.exports.variable = literal | function | object
Here the assigned value (literal | function | object) is
indirectly exposed as a module and can be consumed
using the variable.
Exports Literals
 module.exports = “Hello world example";
save in app.js
 const demo = require("./app");
console.log(demo);
save in index.js
Exports Objects
 module.exports = {
 name: ‘Learn node js',
 website: 'https://nodejs.org'
}

 const demo = require('./app');


 console.log(demo.name);
 console.log(demo.website);
Exports Functions
 module.exports = function (a, b) {
 console.log(a + b);
}

 const sum = require('./app');


 sum(2, 5);
Exports Functions as Class
 module.exports = function () {
 this.name = ‘Hello Demo';
 this.website = 'https://nodjs.org';
 this.info = () => {
 console.log(`Company name - ${this.name}`);
 console.log(`Website - ${this.website}`);
 }
 }

 const Company = require('./app');


 const firstCompany = new Company();
 firstCompany.info();
Exports Multiple Functions
 Store many related functions in one js file
 function add(x, y) { return x + y; }
 function subtract…
 function mutliply …
 function divide…
…
 Module.exports = { add, subtract, multiply, … }
 Use whichever is required
 const f = require('./func');
 const result = f.add(10, 5);
Loading Module from separate
folder
 Navigate to parent or child folder and locate location
of module.
 Examples:
 const index = require('./models/folder/path/index.js');
 const module = require('../../controllers/index.js');
File System Module
 File System module allows you to work with the file
system on your computer.
 var fs = require('fs');
 Common use for the File System module:
 Create
 Read
 Update
 Rename
 Delete
Synchronous v/s Asynchronous
 Synchronous also called blocking functions
It waits for operation to complete, and only after that,
it executes the next operation
 Asynchronous also called non-blocking functions
It never waits for each operation to complete, rather it
executes all operations in the first go itself
 Most asynchronous methods has corresponding
synchronous method and suffixed as “Sync”
 Example – readFile vs readFileSync
Read
 var fs = require("fs");
 // Asynchronous read
 fs.readFile('input.txt', function (err, data) {
 if (err) {
 return console.error(err);
 }
 console.log("Asynchronous read: " + data.toString());
 });
 // Synchronous read
 var data = fs.readFileSync('input.txt');
 console.log("Synchronous read: " + data.toString());
 Observe err if file doesn’t exist
Create, Update, Append
 //If the file does not exist, the file will be created
 fs.appendFile('mynewfile1.txt', 'Hello content!', function (err) {
if (err) throw err;
console.log('Saved!');
});
 //If the file does not exist, an empty file is created
 fs.open('mynewfile2.txt', 'w', function (err, file) {
if (err) throw err;
console.log('Saved!');
});
 //If the file does not exist, a new file, with text, will be created
 fs.writeFile('mynewfile3.txt', 'Hello content!', function (err) {
if (err) throw err;
console.log('Saved!');
});
Other useful operations
 exists()
 existsSync()
 truncate()
 renameSync()
 unlink()
http module – createServer, req, res
fs module – create file, read file, check if file exits
Basic html file
Assignment Objective
 Create a node application which accept a file name in url.
Example – localhost:8080/file.txt
 If file exists in current folder then read file and display content in
browser followed by text “File already exists and contents are:”.
 If file doesn’t exists then create file into current folder and
display content in browser “File does not exists but created
successfully.”. File contents should be “File created on Date”
 Modify application to read html files and display formatted html
into browser.
 Modify application to append file extension like and application
should add html extension automatically.
 Modify application to create separate user defined java-script
functions like Exists, ReadFile, CreateFile. All functions should
be Sync.
Assignment Solution
 var http = require('http');
 var fs = require('fs');

function Exists(file){
 //return true/false
 const existance = fs.existsSync(file);
 return existance;
 }

function ReadFile(file){
 //return content of file or error message
 return fs.readFileSync(file);
 }

function CreateFile(file){
 //create file in data folder and return message
 fs.writeFileSync(file, 'File created on ' + Date());
 }

http.createServer(function (req, res) {
 res.writeHead(200, {'Content-type': 'text/html'});

var fileName = req.url.replace('/', '') + '.html';
 if (Exists(fileName)){
 res.write('File already exists and contents are:')
 var content = ReadFile(fileName);
 res.write(content);
 }
 else{
 res.write('File does not exists but created successfully.')
 var output = CreateFile(fileName);
 }

res.end();
 }).listen(8080);
Assignment Solution
 var http = require('http');
 var fs = require('fs');

function Exists(file){
 //return true/false
 const existance = fs.existsSync(file);
 return existance;
 }

function ReadFile(file){
 //return content of file or error message
 return fs.readFileSync(file);
 }

function CreateFile(file){
 //create file in data folder and return message
 fs.writeFileSync(file, 'File created on ' + Date());
 }

http.createServer(function (req, res) {
 res.writeHead(200, {'Content-type': 'text/html'});

var fileName = req.url.replace('/', '') + '.html';
 if (Exists(fileName)){
 res.write('File already exists and contents are:')
 var content = ReadFile(fileName);
 res.write(content);
 }
 else{
 res.write('File does not exists but created successfully.')
 var output = CreateFile(fileName);
 }

res.end();
 }).listen(8080);
Events and Event Emitters
 Node.js is perfect for event-driven applications.
 Every action on a computer is an event. Like when a connection
is made or a file is opened.
 Node.JS provides way to create and handle custom events.
 The EventEmitter class can be used to create and handle custom
events module.
 2 steps:
 Listening events: Before emits any event, it must register
functions(callbacks) to listen to the events.
 Removing Listener: The eventEmitter.removeListener() takes
two argument event and listener, and removes that listener from the
listeners array that is subscribed to that event.
While eventEmitter.removeAllListeners() removes all the
listener from the array which are subscribed to the mentioned
event.
Handling Events
 Emitting events example
 // Importing events
 var EventEmitter = require('events');

 // Initializing event emitter instances
 var eventEmitter = new EventEmitter();

 // Registering to myEvent
 eventEmitter.on('myEvent', (msg) => {
 console.log(msg);
 });

 // Triggering myEvent
 eventEmitter.emit('myEvent', "First event");
Handling Events
 // Initializing event emitter instances
 var eventEmitter = new EventEmitter();

 var geek1= (msg) => {
 console.log("Message from geek1: " + msg);
 };
 var geek2 = (msg) => {
 console.log("Message from geek2: " + msg);
 };

 // Registering geek1 and geek2
 eventEmitter.on('myEvent', geek1);
 eventEmitter.on('myEvent', geek2);

 // Removing listener geek1 that was
 // registered previously
 eventEmitter.removeListener('myEvent', geek1);

 // Triggering myEvent
 eventEmitter.emit('myEvent', "Event occurred");

 // Removing all the listeners to myEvent
 eventEmitter.removeAllListeners('myEvent');

 // Triggering myEvent
 eventEmitter.emit('myEvent', "Event occurred");
In-built modules Operating System
 The os module provides information about the computer operating system on which node running.

 var os = require('os');

 // return the cpu architecture
 console.log("CPU architecture: " + os.arch());

 // It returns the amount of free system memory in bytes
 console.log("Free memory: " + os.freemem());

 // It return total amount of system memory in bytes
 console.log("Total memory: " + os.totalmem());

 // It returns the list of network interfaces
 console.log('List of network Interfaces: ' + os.networkInterfaces());

 // It returns the operating systems default directory for temp files.
 console.log('OS default directory for temp files : ' + os.tmpdir ());
os module common methods
 arch()Returns the operating system CPU architecture
 cpus()Returns an array containing information about the computer's
CPUs
 freemem()Returns the number of free memory of the system
 hostname()Returns the hostname of the operating system
 loadavg()Returns an array containing the load averages, (1, 5, and 15
minutes)
 platform()Returns information about the operating system's platform
 tmpdir()Returns the operating system's default directory for temporary
files
 totalmem()Returns the number of total memory of the system
 type()Returns the name of the operating system
 uptime()Returns the uptime of the operating system, in seconds
 userInfo()Returns information about the current user
os module
Assignment Objective
 Write application which display following information
on browser
 CPU model
 CPU architecture
 OS platform
 Hint
 We can’t print object in browser directly. Instead use
console to print an object and then grab element to print
desired detail
Assignment Solution
 var http = require('http');
 var os = require('os');

http.createServer(function (req, res) {
 res.writeHead(200, {'Content-type': 'text/html'});
 const cpus = os.cpus();
 res.write('Model is: ' + cpus[0].model);
 res.write('<br/>');
 res.write('Architecture is: ' + os.arch());
 res.write('<br/>');
 res.write('OS Platform is: ' + os.platform());
 res.end();
 }).listen(8080);
Assignment Solution
 var http = require('http');
 var os = require('os');

http.createServer(function (req, res) {
 res.writeHead(200, {'Content-type': 'text/html'});
 const cpus = os.cpus();
 res.write('Model is: ' + cpus[0].model);
 res.write('<br/>');
 res.write('Architecture is: ' + os.arch());
 res.write('<br/>');
 res.write('OS Platform is: ' + os.platform());
 res.end();
 }).listen(8080);
Node.js Buffers
Buffers
 Pure JavaScript is great with strings, but it does not handle
binary data very well.
 The buffers module provides a way of handling streams of
binary data.
 The Buffer object is a global object in Node.js, and it is not
necessary to import it using the require keyword.
 Buffer refers to the particular memory location in memory
 Buffer and array have some similarities, but the difference
is array can be any type, and it can be resizable. Buffers
only deal with binary data, and it can not be resizable.
Create Buffer
 Specified length of octent
var buf = new Buffer(10);
 From a given array
var buf = new Buffer([10, 20, 30, 40, 50]);
 From a string with/without encoding
var buf = new Buffer("Simply Easy Learning", "utf-8");
Write Buffer
 Syntax
buf.write(string[, offset][, length][, encoding])
This method returns the number of octets written

 Example
buf = new Buffer(256);
len = buf.write("Simply Easy Learning");
console.log("Octets written : "+ len);

 Output
Octets written : 20
Reading from Buffer
 Syntax
buf.toString([encoding][, start][, end])
This method decodes and returns a string from buffer data
encoded using the specified character set encoding

 Example
buf = new Buffer(26);
for (var i = 0 ; i < 26 ; i++) { buf[i] = i + 97; }

 Output
console.log( buf.toString('ascii')); //abcdefghijklmnopqrstuvwxyz
console.log( buf.toString('ascii',0,5)); // abcde
console.log( buf.toString('utf8',0,5)); // abcde
Concatenate Buffers
 Syntax
Buffer.concat(list[, totalLength])
This method returns a Buffer instance.

 Example
var buffer1 = new Buffer('TutorialsPoint ');
var buffer2 = new Buffer('Simply Easy Learning');
var buffer3 = Buffer.concat([buffer1,buffer2]);
console.log("buffer3 content: " + buffer3.toString());

 Output
buffer3 content: TutorialsPoint Simply Easy Learning
Compare Buffers
 Syntax
buf.compare(otherBuffer);
Returns a number indicating whether it comes before or after or
is the same as the otherBuffer in sort order.

 Example
var buffer1 = new Buffer('ABC');
var buffer2 = new Buffer('ABCD');
var result = buffer1.compare(buffer2);
if(result < 0) { console.log(buffer1 +" comes before " + buffer2); }
else if(result === 0) { console.log(buffer1 +" is same as " +
buffer2); }
else { console.log(buffer1 +" comes after " + buffer2); }
 Output
ABC comes before ABCD
Copy Buffer
 Syntax
buf.copy(targetBuffer[, targetStart][, sourceStart][,
sourceEnd])
No return value
 Example
var buffer1 = new Buffer('ABC');
//copy a buffer
var buffer2 = new Buffer(3);
buffer1.copy(buffer2);
console.log("buffer2 content: " + buffer2.toString());
 Output
buffer2 content: ABC
Slice Buffer
 Syntax
buf.slice([start][, end])
Returns a new buffer which references the same memory
as the old one, but offset and cropped by the start (defaults
to 0) and end (defaults to buffer.length) indexes
 Example
var buffer1 = new Buffer('TutorialsPoint');
//slicing a buffer
var buffer2 = buffer1.slice(0,9);
console.log("buffer2 content: " + buffer2.toString());
 Output
buffer2 content: Tutorials
Stream Module
 Streams are one of the Node’s best module.
 Streams are collections of data — just like arrays or strings.
 The difference is that streams might not be available all at
once, and they don’t have to fit in memory.
 This makes streams really powerful when working with
large amounts of data, or data that’s coming from an
external source one chunk at a time.
 However, streams are not only about working with big data.
 Example OTT platform, YouTube.
 We can read data from a source or write data to a
destination in continuous way.
Stream types
 Readable − Stream which is used for read operation.
 Writable − Stream which is used for write operation.
 Duplex − Stream which can be used for both read and
write operation.
 Transform − A type of duplex stream where the
output is computed based on input.
Stream Events
 data − This event is fired when there is data is
available to read.
 end − This event is fired when there is no more data to
read.
 error − This event is fired when there is any error
receiving or writing data.
 finish − This event is fired when all the data has been
flushed to underlying system.
Reading from a stream
 var fs = require("fs");
var data = '';
// Create a readable stream
var readerStream = fs.createReadStream('input.txt');
readerStream.setEncoding('UTF8');
// Handle stream events --> data, end, and error
readerStream.on('data', function(chunk)
{ data += chunk; });
readerStream.on('end',function() { console.log(data);
});
readerStream.on('error', function(err) {
console.log(err.stack); });
Reading from a stream
 var fs = require("fs");
var data = '';
// Create a readable stream
var readerStream = fs.createReadStream('input.txt');
readerStream.setEncoding('UTF8');
// Handle stream events --> data, end, and error
readerStream.on('data', function(chunk)
{ data += chunk; });
readerStream.on('end',function() { console.log(data);
});
readerStream.on('error', function(err) {
console.log(err.stack); });
Writing to a stream
 var fs = require("fs");
var data = 'Simply Easy Learning';
// Create a writable stream
var writerStream = fs.createWriteStream('output.txt');
writerStream.write(data,'UTF8');
// Mark the end of file
writerStream.end();
// Handle stream events --> finish, and error
writerStream.on('finish', function()
{ console.log("Write completed."); });
writerStream.on('error', function(err)
{ console.log(err.stack); });
Piping the stream
 We provide the output of one stream as the input to
another stream
 var fs = require("fs");
// Create a readable stream
var readerStream = fs.createReadStream('input.txt');
// Create a writable stream
var writerStream = fs.createWriteStream('output.txt');
// Pipe the read and write operations
// read input.txt and write data to output.txt
readerStream.pipe(writerStream);
Chaining the Streams
 Connect the output of one stream to another stream
and create a chain of multiple stream operations
 Example – Read a file -> compress -> Write to another
file.
 var fs = require("fs");
var zlib = require('zlib');
// Compress the file input.txt to input.txt.gz
fs.createReadStream('input.txt')
.pipe(zlib.createGzip())
.pipe(fs.createWriteStream('input.txt.gz'));
console.log("File Compressed.");
Buffers
Streams
Assignment Objective
 Read 2 text files “file1.txt” and “file2.txt”. Compare
these 2 files using buffer module and display on
console “Both files are same” if their contents are same
else display “Both files are different”.
 Read one text file “file1.txt” and write contents into
another file “file2.txt” using stream.
Node.js RESTAPI
What is API
 An application programming interface (API) defines the rules that you
must follow to communicate with other software systems.
 Mechanisms that enable two software components to communicate
with each other using a set of definitions and protocols
 Developers expose or create APIs so that other applications can
communicate with their applications programmatically.
 API is a gateway between clients and resources on the web.
 Clients - Clients are users who want to access information from the
web. The client can be a person or a software system that uses the API.
 Resources - Resources are the information that different applications
provide to their clients. Resources can be images, videos, text,
numbers, or any type of data.
 Example – Stock market API or weather forecast API
What is REST API
 Representational State Transfer (REST) API is a
software architecture that imposes conditions on how
an API should work
 APIs that follow the REST architectural style are called
REST APIs.
 Web services that implement REST architecture are
called RESTful web services.
Principles of the REST architectural
 Uniform interface - The uniform interface is fundamental to
the design of any RESTful webservice. It indicates that the server
transfers information in a standard format (typically JSON).
 Statelessness - In REST architecture, statelessness refers to a
communication method in which the server completes every
client request independently of all previous requests.
 Layered system - In a layered system architecture, the client can
connect to other authorized intermediaries between the client
and server, and it will still receive responses from the server.
 Cacheability - RESTful web services support caching, which is
the process of storing some responses on the client or on an
intermediary to improve server response time.
 Code on demand - In REST architectural style, servers can
temporarily extend or customize client functionality by
transferring software programming code to the client.
Benefits of RESTful APIs
 Scalability - Systems that implement REST APIs can
scale efficiently because REST optimizes client-server
interactions.
 Flexibility - RESTful web services support total client-
server separation.
 Independence - REST APIs are independent of the
technology used.
How do RESTful APIs work?
 The basic function of a RESTful API is the same as
browsing the internet. These are the general steps for any
REST API call
1. The client sends a request to the server. The client follows
the API documentation to format the request in a way that
the server understands.
2. The server authenticates the client and confirms that the
client has the right to make that request.
3. The server receives the request and processes it internally.
4. The server returns a response to the client. The response
contains information that tells the client whether the
request was successful. The response also includes any
information that the client requested.
HTTP methods/verbs
 Following four HTTP methods are commonly used in
REST based architecture.
 GET − This is used to provide a read only access to a
resource.
 POST − This is used to create a new resource.
 DELETE − This is used to remove a resource.
 PUT − This is used to update a existing resource.
Postman
 Postman is an application for API testing.
 We don’t need to build UI to validate APIs
 We can share APIs across teams.
 Download from
https://www.postman.com/downloads/
 Explain with help of postman
 Methods/verbs
 Response code
 API collections
Explain postman
Express module
 Express.js is a web framework for Node.js.
 It is
 Fast
 Robust
 Asynchronous in nature.
 MVC like structure
 Ultra fat I/O
Express module
 Express is a minimal and flexible Node.js web
application framework that provides a robust set of
features for web and mobile applications.
 Dozens of popular framework are based on express
module itself for example NestJS, Kites, LoopBack,
etc…
 This is not an inbuild module.
 We need to install express using “npm install express”
HTTP v/s Express module
 HTTP: It is an in-build module which is pre-installed along
with NodeJS. It is used to create server and set up
connections. Using this connection, data sending and
receiving can be done as long as connections use a
hypertext transfer protocol.
 Express: Express as a whole is known as a framework, not
just as a module. It gives you an API, submodules,
functions, and methodology and conventions for quickly
and easily typing together all the components necessary to
put up a modern, functional web server with all the
conveniences necessary for that (static asset hosting,
templating, handling CSRF, CORS, cookie parsing, POST
data handling, and many more functionalities.
Install using “npm install express”
HTTP v/s Express module
 HTTP example
 var http = require('http');
 http.createServer(function (req, res) {
 res.write('Hello World!');
 res.end();
 }).listen(3000);

 Express example
 const express = require('express');
 const app = express();
 app.get('/', function (req, res) {
 res.send("Hello World!, I am server created by expresss");
 })
 app.listen(3000, function () {
 console.log("server started");
 })
JSON schema (user.json)
 Create a JSON with schema like
[
 {
 "id": 1 ,
 "name" : "mahesh",
 "password" : "password1",
 "profession" : "teacher"
 },
 {
 "id": 2 ,
 "name" : "dinesh",
 "password" : "password2",
 "profession" : "faculty"
 },
 {
 "id": 3 ,
 "name" : "mahesh",
 "password" : "password1",
 "profession" : "teacher"
 }
 ]
Setup express
 const express = require('express');
 const fs = require('fs');
 const cors = require('cors’); //Cross-Origin Resource Sharing
 const app = express();
 app.use(cors());

 // APIs will be created here

 app.listen(3000, function () {
 console.log("server started");
 })
Get Operation
 app.get('/listUsers', function (req, res) {
 fs.readFile("user.json", 'utf8', function (err, data){
 if(err){
 console.log("Error " + err);
 }
 else{
 console.log( data );
 res.end( data );
 }
 });
 });
Post Operation
 app.post('/addUser', function (req, res) {
 fs.readFile("user.json", 'utf8', function (err, data) {
 if(err){
 console.log("Error " + err);
 }
 else{
 data = JSON.parse(data);
 data.push(req.body.user); // Save data into user.json
 console.log(data);
 res.end(JSON.stringify(data));
 res.end();
 }
 })
 });
Fix problem of POST
 Add following middleware
 app.use(express.json());
 app.use(express.urlencoded({ extended: false }));
 Middleware gets executed after the server receives the request
and before send the response.
 The objective of express.json() is used to parse the incoming
requests with JSON payloads and is based upon the body-parser.
 The objective of express.urlencoded() method is to parse the
incoming request with urlencoded payloads and is based upon
the body-parser.
 Middleware can be used to add logging and authentication
functionality.
 Example – Without middleware we have to write logic in every
API to authenticate user.
How middleware work
Middleware
 Middleware Chaining: Middleware can be chained
from one to another, Hence creating a chain of
functions that are executed in order.
 The last function sends the response back to the
browser.
 The next() function in the express is responsible for
calling the next middleware function if there is one.
Creating own middleware
 We can create our own middleware
 const myLogger = function (req, res, next) {
 console.log('LOGGED')
 next()
 }
 module.exports = myLogger

 Save above middleware in a file


 Use this file into your nodeJS application
 Use middleware using app.use(myLogger);
Detail Operation
 app.get('/:id', function (req, res) {
 fs.readFile("user.json", 'utf8', function (err, data){
 if(err){
 console.log("Error " + err);
 }
 else{
 var users = JSON.parse( data );
 var user = users[req.params.id]
 console.log( user );
 res.end( JSON.stringify(user));
 }
 });
 });
Delete Operation
 app.delete('/deleteUser/:id', function (req, res) {
 fs.readFile("user.json", 'utf8', function (err, data) {
 if(err){
 console.log("Error " + err);
 }
 else{
 data = JSON.parse(data);
 data.splice(req.params.id, 1); // Save data into user.json
 console.log(data);
 res.end(JSON.stringify(data));
 res.end();
 }
 });
 })
Correct way of route
 app.get(‘/user’, …)
 app.post(‘/user’, …)
 app.get(‘/user/:id’, …)
 app.delete(‘/user/:id, …)
Routing
 Routing defines the way in which the client requests
are handled by the application endpoints.
 In another way how an application responds to a client
request to a particular endpoint, which is a URI (or
path) and a specific HTTP request method (GET,
POST, and so on).
 Each route can have one or more handler functions,
which are executed when the route is matched.
Routing
 Route definition takes the following structure:
 app.METHOD(PATH, HANDLER)
 app is an instance of express.
 METHOD is an HTTP request method, in lowercase.
 PATH is a path on the server.
 HANDLER is the function executed when the route is
matched. (also called callback functions)
Routing
 Route methods –
 A route method is derived from one of the HTTP
methods, and is attached to an instance of the express
class (app);
 Route paths
 Route paths, in combination with a request method,
define the endpoints at which requests can be made.
Route paths can be strings, string patterns, or regular
expressions. Example app.get('/ab*cd’, …)
Routing
 Route parameters
 Route parameters are named URL segments that are used to capture
the values specified at their position in the URL.
 Example
 app.get('/users/:userId/books/:bookId', (req, res) => {
 res.send(req.params)
 })
 userId and bookId are parameters and can be replaced anything.
 This is a valid route for this example
 http://localhost:3000/users/34/books/8989
 We can grab route parameters using “req.params”.
 Above example will return following output with “req.params”
 { "userId": "34", "bookId": "8989" }
 Another example is – “/flights/:from-:to” – Discuss this.
 We can handle routing in 2 ways
 Without using framework
 Using express framework
Routing Without Framework
 http.createServer(function (req, res) {
var url = req.url;
if(url ==='/about') {
res.write(' Welcome to about us page');
res.end();
} else if(url ==='/contact') {
res.write(' Welcome to contact us page');
res.end();
} else {
res.write('Hello World!');
res.end();
}
}).listen(3000, function() {
console.log("server start at port 3000");
});
Routing With Framework
 Use get, post, delete methods with URL
 app.get('/', function(req, res) {
res.send('Hello Get')
})
 app.post('/', function(req, res) {
res.send('Hello Post')
})
 app.put('/', function(req, res) {
res.send('Hello Put')
})
 Use app.all() to handle all request.
Request object
 Request object represents the HTTP request and has
properties for the request
 query string
 parameters
 Body
 HTTP headers
 Etc…
Request object properties
Properties & Description

req.app - This property holds a reference to the instance of the express application that is using the middleware.

req.baseUrl - The URL path on which a router instance was mounted.

req.body - Contains key-value pairs of data submitted in the request body. By default, it is undefined, and is
populated when you use body-parsing middleware such as body-parser

req.cookies - When using cookie-parser middleware, this property is an object that contains cookies sent by the
request.

req.fresh - Indicates whether the request is "fresh." It is the opposite of req.stale.

req.hostname - Contains the hostname from the "Host" HTTP header.

req.ip - The remote IP address of the request.

req.ips - When the trust proxy setting is true, this property contains an array of IP addresses specified in the “X-
Forwarded-For” request header.

req.originalUrl - This property is much like req.url; however, it retains the original request URL, allowing you to
rewrite req.url freely for internal routing purposes.

req.params - An object containing properties mapped to the named route “parameters”. For example, if you have
the route /user/:name, then the "name" property is available as req.params.name. This object defaults to {}.
Request object properties
Properties & Description

req.path - Contains the path part of the request URL.

req.protocol - The request protocol string, "http" or "https" when requested with TLS.

req.query - An object containing a property for each query string parameter in the route.

req.route - The currently-matched route, a string.

req.secure - A Boolean that is true if a TLS connection is established.

req.signedCookies - When using cookie-parser middleware, this property contains signed cookies sent by the
request, unsigned and ready for use.

req.stale - Indicates whether the request is "stale," and is the opposite of req.fresh.

req.subdomains - An array of subdomains in the domain name of the request.

req.xhr - A Boolean value that is true if the request’s "X-Requested-With" header field is “XMLHttpRequest”,
indicating that the request was issued by a client library such as jQuery.
Request object methods
 req.accepts(types) - This method checks if the specified content
types are acceptable
req.accepts('html');
 req.get(field) - This method returns the specified HTTP request
header field
req.get('Content-Type');
 req.is(type) - This method returns true if the incoming request’s
"Content-Type" HTTP header field matches the MIME type
specified by the type parameter.
req.is('text/html');
 req.param(name [, defaultValue]) - This method returns the
value of param name when present.
// ?name=abc
req.param('name') // => “abc"
Response object properties
Properties & Description
res.app - This property holds a reference to the instance of the express
application that is using the middleware.
res.headersSent - Boolean property that indicates if the app sent HTTP headers
for the response.
res.locals - An object that contains response local variables scoped to the request
Response object methods
 res.append(field [, value]) - This method appends the specified value to the HTTP response header
field.
res.append('Warning', '199 Miscellaneous warning');
 res.attachment([filename]) - This method is used to send a file as an attachment in the HTTP
response.
res.attachment('path/to/logo.png');
 res.cookie(name, value [, options]) - This method is used to set cookie name to value. The value
parameter may be a string or object converted to JSON.
res.cookie('cart', { items: [1,2,3] });
 res.clearCookie(name [, options])
 res.download(path [, filename] [, fn])
 res.end([data] [, encoding])
 res.format(object)
 res.get(field) - This method is used to return the HTTP response header specified by field.
res.get('Content-Type');
 res.json([body]) - This method is used to send a JSON response.
res.json({ user: 'tobi' })
 res.jsonp([body]) - This method is used to send a JSON response with JSONP support
JSONP is a method for sending JSON data without worrying about cross-domain issues.
Response object methods
 res.links(links) - This method is used to join the links provided as properties of
the parameter to populate the response’s Link HTTP header field.
res.links ({ next: 'http://api.example.com/users?page=2', last:
'http://api.example.com/users?page=5' });
 res.location(path)
 res.redirect([status,] path)
 res.render(view [, locals] [, callback])
 res.send([body])
 res.sendFile(path [, options] [, fn])
 res.sendStatus(statusCode)
 res.set(field [, value]) - This method is used to set the response’s HTTP header
field to value.
res.set('Content-Type', 'text/plain');
 res.status(code)
 res.type(type)
AJAX
 Asynchronous JavaScript And XML
 AJAX is not a programming language
 AJAX can be used to
 Update a web page without reloading
 Request data from a server (backend like Node.JS)
 Send data to server
 A browser built-in XMLHttpRequest object
Sample code to update dynamically
 <html>
 <head>
 <script lang="javascript">
 function loadDoc(){
 document.getElementById("dynamic").innerHTML = "This is dynamic text " +
Date();
 }
 </script>
 </head>
 <body>
 <div id="demo">
 <h2>Let AJAX change this text</h2>
 <button type="button" onclick="loadDoc()">Change Content</button>
 </div>
 <div id="dynamic"></div>
 </body>
 </html>
Pure java-script example
 function loadDoc() {
 var xhr = new XMLHttpRequest();
 var url = 'https://jsonplaceholder.typicode.com/todos/1';
 xhr.open("GET", url, true);
 xhr.onreadystatechange = function () {
 if (this.readyState == 4 && this.status == 200) {
 console.log(this.responseText);
 }else{
 console.log("Some error occured.")
 }
 }
 // Sending our request
 xhr.send();
 }
readyState values
 0: request not initialized
1: server connection established
2: request received
3: processing request
4: request finished and response is ready
Jquery example
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.
min.js"></script>

 function loadDoc() {
 $.ajax({
url: "https://jsonplaceholder.typicode.com/todos",
 success: function(result) {
 console.log(result);
 },
 error : function(result){
 console.log("Error "+result);
 }
 });
 }
Node.JS + Jquery example
 Steps
 Create fake data store (user.json)
 Create GET route to read user.json and return to client
 Enable CORS (Cross-Origin Resource Sharing) - CORS allows
a server to indicate any origins (domain, scheme, or port)
other than its own from which a browser should permit
loading resources.
 Run the server application
 Create client application to consume backend data.
 Run client application
Step 1 - Create fake data store
{
 "user1" :
 { "name" : "mahesh",
 "password" : "password1",
 "profession" : "teacher",
 "id": 1
 }
}
Step 2 - Create GET route
 const express = require('express');
 const fs = require('fs');
 const cors = require('cors');
 const app = express();

app.use(cors());

app.get(‘/user', function (req, res) {
 fs.readFile("user.json", 'utf8', function (err, data){
 if(err){
 console.log("Error " + err);
 }
 else{
 console.log( data );
 res.end( data );
 }
 });
 })
 app.listen(3000, function () {
 console.log("server started");
 })

Step 3 – Run the server
 node index.js
 It will launch backend at http://localhost:3000/user
Step 4 – Create client application
 function loadDoc() {
 $.ajax({
 headers: { "Accept": "application/json"},
 type: 'GET',
 url: "http://localhost:3000/user",
 success: function(result) {
 console.log(result);
 },
 error : function(result){
 console.log("Error "+result);
 }
 });
 }
Step 5 – Run client application
 Open HTML file in browser and call the function.
Steps to use MySQL in NodeJS
 Install mysql driver
 npm install mysql
 Use mysql in application
 var mysql = require('mysql’);
 Create connection
 var con = mysql.createConnection({
 host: "localhost",
 user: "yourusername",
 password: "yourpassword"
 });

 con.connect(function(err) {
 if (err) throw err;
 console.log("Connected!");
 });
 Query database
 con.query(sql, function (err, result) {
 if (err) throw err;
 console.log("Result: " + result);
 });
 Do all operations like create DB, create table, insert, update, delete, query, sort, where, group, etc…
More on modules
Types of modules
 Core modules
 Built-in modules that are part of Node.js and come with
the Node.js installation process (Java script framework)
are known as core modules.
 Local modules
 Local modules are created by user Node.js application.
These modules are included in our program in the same
way as we include the built in module.
 Third party modules
 Modules that are available online and are installed using
the npm command are called third party modules.
Core modules
 Few core modules are
 Http: Used for creating an HTTP server in nodejs.
 Url: This module also helps in parsing the urls.
 Path: It helps to find the file paths.
 Querystring: It helps in parsing and proper formatting
of the url.
 Process: Gives information regarding the current
process.
 Fs: Used in modification of files.
 Os: It contains the details of the operating system in
which the node js application is currently running.
http module
 METHODS property
 Try console.log(http.METHODS) and validate output
 It provides more than 2 dozens supported methods.
 Generally we use 4/5 methods
 Few common methods
 GET- Requests a specified resource.
 HEAD - Asks for a response identical to a GET request, but without
the response body.
 POST - Submits an entity to the specified resource.
 PUT - Replaces all of the target resource with the request payload.
 DELETE - Deletes the specified resource.
 PATCH - The PATCH method applies partial modifications to a
resource.
http module
 STATUS_CODE property
 Try console.log(http.STATUS_CODE) and validate output
 It provides more than 3-4 dozens supported status code.
 Range wise updates
 Informational responses (100 – 199)
 Successful responses (200 – 299)
 Redirection messages (300 – 399)
 Client error responses (400 – 499)
 Server error responses (500 – 599)
 Few commonly used status code
 200 OK - a successful request.
 201 Created - This indicates that the request was completed, and a new resource was created as a result
 204 No Content - This response is sent when the server has completed the request and does not need to return an entity-body
 400 Bad Request - When the server is unable to understand the request due to incorrect syntax
 401 Unauthorized – User is not authorized to access resource. (No credential provided)
 403 Forbidden - when the server refuses to provide the requested resource because the client does not have access permissions.
(Credential provided but not enough privileges)
 404 Not Found - The requested resource is unavailable on the server
 500 Internal Server Error - The server ran into an unanticipated problem that stopped it from completing the request.
 503 Service not available - This is due to a sudden overload and it means the server cannot handle the request.
 504 Gateway timeout - When the server is operating as a gateway and cannot promptly respond, this error response is returned.
http module
 createServer method
 We can use it to create a server. The create server will
take a function which will run when we try to access the
port.
 listen method
 starts the HTTP server and listens for connections.
http module
 Adding headers
 We have to set header before send response.
 Based on processing we can send any header code.
 Use writeHead method of response
 Example – readFile and file doesn’t exists.
 Reading query string
 To grab url use req.url property of request object.
url module
 It breaks down the url into readable parts
 It gives fine tune control of url
 let url = require('url');
 var adr =
'http://localhost:3000/search?year=2021&month=august';
 var q = url.parse(adr, true); //parse query string or not
 console.log(q.host);
 console.log(q.pathname); // /search
 console.log(q.search); // ?year=2021&month=august
 var qdata = q.query;
 console.log(qdata.month); // august
Path module
 Path module is used for handling and transforming
file paths.
 Extract file name, extensions, join may be difficult if
we handle file name as strings.
 Example
 Ext = “.htm” (grab from url)
 path = “/dir1/dir2/” + “file1.” + ext
 Problem may occur if we miss any slash while creating
path.
 var path = require("path")
Path module
 Main methods
 path.normalize(p) - Normalize a string path, taking care of '..' and '.' parts.
 path.join([path1][, path2][, ...]) - Join all the arguments together and normalize the
resulting path.
 path.resolve([from ...], to) - Resolves to an absolute path.
 path.isAbsolute(path) - Determines whether the path is an absolute path. An absolute
path will always resolve to the same location, regardless of the working directory.
 path.relative(from, to) - Solve the relative path from from to to.
 path.dirname(p) - Return the directory name of a path. Similar to the Unix dirname
command.
 path.basename(p[, ext]) - Return the last portion of a path. Similar to the Unix basename
command.
 path.extname(p) - Return the extension of the path, from the last '.' to end of string in the
last portion of the path. If there is no '.' in the last portion of the path or the first character
of it is '.', then it returns an empty string.
 path.parse(pathString) - Returns an object from a path string.
 path.format(pathObject) - Returns a path string from an object, the opposite of path.parse
above.
Path module
 Main properties
 path.sep - The platform-specific file separator. '\\' or '/'.
 path.delimiter - The platform-specific path delimiter, ;
or ':'.
 path.posix - Provide access to aforementioned path
methods but always interact in a posix compatible way.
 path.win32 - Provide access to aforementioned path
methods but always interact in a win32 compatible way.
Path module
 // Normalization
 console.log(path.normalize(‘/dir1/dir2//dir3/dir4/file/..'));
 Output - /dir1/dir2/dir3/dir4

 // Join
 console.log(path.join(‘/dir1', ‘dir2’, dir3/dir4', ‘file', '..’));
 Output - /dir1/dir2/dir3/dir4

 // Resolve
 console.log(path.resolve('main.js’));
 Output - /dir1/dir1/main.js

 // extName
 console.log(path.extname('main.js’));
 Output - .js
Querystring module
 We can use url module to work with querystring.
 But querystring module provide fine control on querystrings
 A query string is a part of the uniform resource locator (URL),
that assigns values to specified parameters.
 The string after the ? in a url. Some url examples are shown
below.
 Example –
http://localhost:3000/product?name=dress&color=red
 Here we have 2 query string parameters
 Name with value dress
 Color with value red
 Using query string we can deal different situations with single
resource.
Querystring module
 const querystring = require(‘querystring’)
 Methods:
 querystring.decode() – Same as parse
 querystring.encode() – Same as stringify
 querystring.escape(str)
 It produce a percent-encoded query string from a normal string. Will
print %20 for any spaces into str.
 querystring.unescape(str)
 Reverse of escape
 querystring.parse(str[, sep[, eq[, options]]])
 Used to parse the URL query string into an object that contains the key
value pair.
 querystring.stringify(obj[, sep[, eq[, options]]])
 Used to produce a query string from a given object, which contains a key
value pair.
Querystring module
 let urlQueryString =
"name=demo&units=kgs&units=pounds&login=false";
 let parsedObj = querystring.parse(urlQueryString);
 console.log("Parsed Query 1:", parsedObj);
 Output
 { name: ‘demo', units: [ 'kgs', 'pounds' ], login: 'false’ }
 Similar example for stringfy
Process module
 The process object (which is an instance of the
EventEmitter) is a global variable that provides
information on the currently running Node.js process.
 As the process module is an EventEmitter, you can
subscribe to its events just like you do it with any other
instances of the EventEmitter using the .on call:
 process.on('eventName', () => {
 //do something
 })
Process module
 To handle uncaughtException
 process.on('uncaughtException', (err) => {
 //log exception
 })
 When exit application
 process.on('exit', code => {
 //log anything
 });
Fs module
 Read a file and validate content into postman (not
browser and console)
 Chagne writeHead to ‘application/json’
 JSON.stringify({ data: contents_of_file })
Content of a file as JSON
 http.createServer(function (req, res) {
 res.writeHead(200, {'Content-type': 'application/json'});

 var fileName = req.url.replace('/', '') + '.html';


 if (Exists(fileName)){
 //res.write('File already exists and contents are:')
 var content = ReadFile(fileName);
 res.end(JSON.stringify({ data: content.toString() }));
 }
 else{
 res.write('File does not exists but created successfully.')
 var output = CreateFile(fileName);
 }

 res.end();
 }).listen(8080);
Os module
 Same as earlier slides
More on buffers and Streams
Buffers and Streams
 Buffer objects are used to represent a fixed-length sequence of
bytes.
 Buffers are designed to handle binary raw data.
 A binary stream is a collection of large amounts of binary data.
 Due to their massive size, binary streams are not sent together.
Instead, they are broken into smaller pieces before sending.
 When the data processing unit cannot accept any more data
streams, excess data is stored in a buffer until the data processing
unit is ready to receive more data.
 Streams of data being sent to the receiver need to be stored
somewhere until the receiver is ready to take in more chunks of
data for processing. This is where the Node.js buffer class comes
into play.
Buffer methods
 Buffer.alloc(size) – This method creates a new buffer of any
size. When you use this method, you assign the size of the
buffer in bytes.
 const buf = Buffer.alloc(6);
 console.log(buf);
 // This will print <Buffer 00 00 00 00 00 00>
 Buffer.write(string) - method writes a string to the buffer,
which can be useful when you need to stream strings in the
form of buffers.
 const buf = Buffer.alloc(100); // Creating a new Buffer
 const len = buf.write("Hello world!"); // Writing to the Buffer
 // len is now 12
Buffer methods
 Buffer.byteLength(buf) – To check the length of a buffer object
 var buf = Buffer.alloc(6);
 var buffLen = Buffer.byteLength(buf );
 console.log(buffLen);
 // This will print <6>
 Buffer.compare() – Already discussed
 Buffer.concat() – Already discussed
 buf.entries() – This return a loop of indexes and bytes from the content of a
buffer object
 var buf = Buffer.from('xyz');
 for (a of buf.entries()) {
 /*This will print arrays of indexes and byte of buffer content
 \\[ 0, 120 \][ 1, 121 \][ 2, 122 ]*/
 console.log(a);
 }
Buffer methods
 Buffer.fill(value) – Used to create a buffer, allocate a size,
and fill it with a specified value.
 const b = Buffer.alloc(10).fill('a');
 console.log(b.toString());
 // This will print aaaaaaaaaa
 Buffer.from(object[, offsetOrEncoding[,length]]) – It
enables you to create a new buffer from any object, like
strings, buffer, arrays, and ArrayBuffer()
 buff.includes() - If you want to determine whether a buffer
object contains any values, you can use this method.
 const buf = Buffer.from('this is a buffer');
 console.log(buf.includes('this'));
 // This will print true
Buffer methods
 Buffer.isEncoding() – It is used to confirm whether a particular
character encoding type is supported
 console.log(Buffer.isEncoding('hex'));
 // This will print true
 Buffer.slice() – already discussed
 Buffer swap – It used to swap the byte order of a buffer object. We can
use buf.swap16(), buf.swap32(), and buf.swap64() methods.
 buf1.swap16();
 console.log(buf1);
 //This will swap 1-2, 3-4, and so on characters.
 buf.toJSON() – It returns a JSON version of the buffer object.
 const buf = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]);
 console.log(buf.toJSON());
 // This will print {"type":"Buffer", data:[1,2,3,4,5,6,7,8]}
Streams
 We have done lot’s of practice using streams. Let’s
discuss some practical example.
 Streams are collections of data — just like arrays or
strings.
 The difference is that streams might not be available
all at once, and they don’t have to fit in memory.
 This makes streams really powerful when working with
large amounts of data, or data that’s coming from an
external source one chunk at a time.
Create a normal file
 const fs = require('fs');
 const file = fs.createWriteStream('./big.file');
 for(let i=0; i<= 10000; i++) { //Approx 4MB of file.
 file.write(‘Write a string approx. 500 character
length\n’);
}
 file.end();
Read normal file
 const fs = require('fs');
 const server = require('http').createServer();

 server.on('request', (req, res) => {


 fs.readFile('./big.txt', (err, data) => {
 if (err) throw err;
 res.end(data);
 });
 });

 server.listen(8000);
Create and read big file
 Change 10000 to 1e6.
 Create file again.
 Try to read file again.
 Reason - We basically put the whole big.txt content in
memory before we wrote it out to the response object.
This is very inefficient.
Solution to big file
 const fs = require('fs');
 const server = require('http').createServer();

 server.on('request', (req, res) => {


 const src = fs.createReadStream('./big.txt');
 const target = fs.createWriteStream('./trg.txt');
 src.pipe(target);
 res.end('File processed');
 });

 server.listen(8000);
Create a CRUD operations using MySQL DB
Create a CRUD RestAPIs
 To implement CRUD (Create, Read, Update, Delete)
operations in Node.js with MySQL database, you will
need to follow these steps:
 Install the necessary dependencies
 Create a MySQL database and table
 Create a Node.js server
 Test CRUD in front-end
Install dependencies
 express: A Node.js web application framework.
 mysql: A MySQL client for Node.js.
 npm install express mysql
Sample DB details
 Db4free.net
 User satyapriya
 Pass Password@123
 Database demojecrc
Database
 CREATE DATABASE IF NOT EXISTS demojecrc;
 USE demojecrc;

 CREATE TABLE IF NOT EXISTS users (


 id INT(11) NOT NULL AUTO_INCREMENT,
 name VARCHAR(50) NOT NULL,
 email VARCHAR(100) NOT NULL,
 password VARCHAR(100) NOT NULL,
 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
 updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP,
 PRIMARY KEY (id)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Steps
 Use express, mysql, cors
 Add middleware cors
 Use express.json() and extended to deal with json
 Connect with mysql
 Create get users route
 Create post users route
 Create update user route
 Create delete user route
 Start server and test applicaiton
Next step:
 Observe problem of a big file and maintenance
problem
 Resolve above.
Create different modules
 Create a folder CRUD and add these classes
 Connection.js
 Get.js
 Post.js
 Update.js
 Delete.js
 GetOne.js
 Use all into crud.js
Connectin.js
 const mysql = require('mysql');

 // Create a MySQL connection


 const connection = mysql.createConnection({
 host: '85.10.205.173',
 port: 3306,
 user: 'satyapriya',
 password: 'Password@123',
 database: 'demojecrc'
 });

 // Connect to the MySQL server
 connection.connect((error) => {
 if (error) {
 console.error('Error connecting to the database:', error);
 } else {
 console.log('Connected to the database');
 }
 });

 module.exports = connection;
Get.js
 const connection = require('./connection');
 const get = (req, res) => {
 // Retrieve all users from the database
 const sql = 'SELECT * FROM users';
 connection.query(sql, (error, results) => {
 if (error) {
 console.error('Error retrieving users from the database:', error);
 res.status(500).send('Error retrieving users from the database');
 } else {
 res.send(results);
 }
 });
 }

 module.exports = get;
Post.js
 const connection = require('./connection');

 const post = (req, res) => {


 // Create a new user in the database
 const { name, email, password } = req.body;
 const sql = 'INSERT INTO users (name, email, password) VALUES (?, ?, ?)';
 connection.query(sql, [name, email, password], (error, results) => {
 if (error) {
 console.error('Error creating a new user in the database:', error);
 res.status(500).send('Error creating a new user in the database');
 } else {
 res.send(results);
 }
 });
 }

 module.exports = post;
Update.js
 const connection = require('./connection');

 var update = (req, res) => {


 // Update an existing user in the database
 const { id } = req.params;
 const { name, email, password } = req.body;
 const sql = 'UPDATE users SET name = ?, email = ?, password = ? WHERE id = ?';
 connection.query(sql, [name, email, password, id], (error, results) => {
 if (error) {
 console.error('Error updating an existing user in the database:', error);
 res.status(500).send('Error updating an existing user in the database');
 } else {
 res.send(results);
 }
 });
 };

 module.exports = update
Delete.js
 const connection = require('./connection');

 const deleteOne = (req, res) => {


 // Delete an existing user from the database
 const { id } = req.params;
 const sql = 'DELETE FROM users WHERE id = ?';
 // Execute the SQL query
 connection.query(sql, [id], (error, results) => {
 if (error) {
 console.error('Error deleting an existing user from the database:', error);
 res.status(500).send('Error deleting an existing user from the database');
 } else {
 res.send(results);
 }
 });
 };
 module.exports = deleteOne;
GetOne.js
 const connection = require('./connection');

 const getOne = (req, res) => {


 // Update an existing user in the database
 const { id } = req.params;
 const sql = 'SELECT * FROM users WHERE id = ' + id;
 connection.query(sql, (error, results) => {
 if (error) {
 console.error('Error retrieving user from the database:', error);
 res.status(500).send('Error retrieving users from the database');
 } else {
 res.send(results);
 }
 });
 }

 module.exports = getOne;
Crud.js
 const express = require('express');
 const cors = require('cors');

 const app = express();

 app.use(cors());
 app.use(express.json());
 app.use(express.urlencoded({ extended: false }));

 // Set up the routes


 const get = require('./get');
 app.get('/users', get);

 const post = require('./post');


 app.post('/users', post);

 const update = require('./update');


 app.put('/users/:id', update);

 const deleteOne = require('./delete');


 app.delete('/users/:id', deleteOne);

 const getOne = require('./getOne');


 app.get('/users/:id', getOne);

 // Start the server


 const port = 3000;
 app.listen(port, () => {
 console.log(`Server is running on port ${port}`);
 });
Create postman collection
 GET users
 GET one user
 POST users
 UPDATE users
 Delete users
Get users
Get one user
Add user
Update user
Delete user
Next Step
 Add Logger middleware
 This will log every request url and method used
 Use req.url and req.method
 Store url and method into a database table
 Add authentication middleware
 If header token is ‘dummy’ then request will be served,
otherwise not.
 Use req.headers[‘token’]
 Send unauthorised if token is invalid.
Create Authentication before doing any operation

You might also like