U4-01-Node JS
U4-01-Node JS
UNIT-IV
Node JS
Node JS - https://nodejs.org/en/
Node.js® is an open-source, cross-platform JavaScript runtime environment .
Node JS
Node.js is an open-source and cross-platform runtime environment
built on Chrome’s V8 JavaScript engine for executing JavaScript code
outside of a browser.
NodeJS is not a framework and it’s not a programming language.
To run JS file:
$ node app.js
Node JS Modules
In NodeJS, Modules are the blocks of encapsulated code that
communicates with an external application on the basis of their related
functionality.
Node JS Documentation
https://nodejs.org/docs/latest-v17.x/api/
NodeJS Local Modules
Local modules are created by us locally in our Node.js application.
These modules are included in our program in the same way as we
include the built in module.
execution.js mathcal.js
NodeJS Third Party Modules
Modules that are available online and are installed using the npm are
called third party modules.
Examples of third party modules are express, mongoose, Amazon Alexa
etc.
Export Module in Node.js
If you have an older version of NPM then you can update it to the
terminal window.
$ npm list -g
Update Package
Uninstall Packages
require
Create Server
server.
NodeJS Web Server
HTTP Header If the response from the HTTP server is supposed to be
displayed as HTML, you should include an HTTP header with the correct
content type.
The first argument of the res.writeHead() method is the status
code, 200 means that all is OK, the second argument is an object
containing the response headers.
Port Number
HTTP server that listens to server ports and gives a response back
to the client.
NodeJS Web Server
NodeJS Web Server-Handle HTTP Request
NodeJS Web Server
NodeJS File System - fs
Node.js includes fs module to access physical file system. The fs module is responsible for
all the asynchronous or synchronous file I/O operations.
To include the File System module, use the require() method:
var fs = require('fs');
Common use for the File System module:
1. Read files
2. Create files
3. Update files
4. Delete files
5. Rename files
NodeJS File System - Synchronous
the program until the file operation is performed. These functions are
fs.readFileSync()
NodeJS File System - Asynchronous
and each command is executed after the previous command even if the
The previous command runs in the background and loads the result once
Parameter Description:
options: The options parameter can be an object or string which can include
encoding and flag. The default encoding is utf8 and default flag is "r".
callback: A function with two parameters err and data. This will get called when
index.js
NodeJS File System - fs
Use fs.writeFile() method to write data to a file. If file already exists then it overwrites the
existing content otherwise it creates a new file and writes data into it.
Parameter Description:
options: The options parameter can be an object or string which can include encoding,
mode and flag. The default encoding is utf8 and default flag is "r".
callback: A function with two parameters err and data. This will get called when write
operation completes.
NodeJS File System - fs
NodeJS File System - fs
The fs.appendFile() method is used to synchronously append the data to the file.
Parameters:
data: It is mandatory and it contains the data that you append to the file.
Callback: Function is mandatory and is called when appending data to file is completed.
NodeJS File System - fs
NodeJS File System - fs
The fs.open() method is used to create, read, or write a file.
Parameters:
path: It holds the name of the file to read or the entire path if stored at other
locations.
mode: Sets the mode of file i.e. r-read, w-write, r+ -readwrite (Default).
NodeJS File System - fs
NodeJS File System - fs
Flag Description
r Open file for reading. An exception occurs if the file does not exist.
r+ Open file for reading and writing. An exception occurs if the file does not exist.
rs Open file for reading in synchronous mode.
rs+ Open file for reading and writing, telling the OS to open it synchronously. See notes for 'rs'
about using this with caution.
w Open file for writing. The file is created (if it does not exist) or truncated (if it exists).
wx Like 'w' but fails if path exists.
w+ Open file for reading and writing. The file is created (if it does not exist) or truncated (if it
exists).
wx+ Like 'w+' but fails if path exists.
a Open file for appending. The file is created if it does not exist.
ax Like 'a' but fails if path exists.
a+ Open file for reading and appending. The file is created if it does not exist.
ax+ Like 'a+' but fails if path exists.
NodeJS File System - fs
fs.unlink(path, callback);
NodeJS File System - fs
NodeJS Event Emitter class
Every action on a computer is an event. Like when a connection is
made or a file is opened.
Node.js allows us to create and handle custom events easily by using
events module. Event module includes EventEmitter class which can
be used to raise and handle custom events.
const EventEmitter = require('events');
eventEmitter.addListener(event, listener)
eventEmitter.on(event, listener)
NodeJS Event Emitter class
NodeJS Event Emitter class
NodeJS Event Emitter class
NodeJS Event Emitter class
Common Patterns for EventEmitters
There are two common patterns that can be used to raise and bind an event using
EventEmitter class in Node.js.
Return EventEmitter from a function
In this pattern, function returns an EventEmitter object, which was used to emit
events inside a function. This EventEmitter object can be used to subscribe for the
events.
Extend the EventEmitter class
In this pattern, we can extend the constructor function from EventEmitter class to
emit the events.
EventEmitter class using util.inherits() method of utility module.
NodeJS Returning Event Emitter.
NodeJS Inheriting Events