NodeJS Master - Notes
NodeJS Master - Notes
JS
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());
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
req.app - This property holds a reference to the instance of the express application that is using the middleware.
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.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.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.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.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'});
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.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.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;
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');
module.exports = post;
Update.js
const connection = require('./connection');
module.exports = update
Delete.js
const connection = require('./connection');
module.exports = getOne;
Crud.js
const express = require('express');
const cors = require('cors');
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: false }));