NodeJS PPT

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 30

NODE.

JS Module 4
INTRODUCTION:
• Node.js is an open-source server environment.
• Node.js allows you to run JavaScript on the server.

• Node.js can generate dynamic page content


• Node.js can create, open, read, write, delete, and close files
on the server
• Node.js can collect form data
• Node.js can add, delete, modify data in your database
ENVIRONMENT SETUP:
• Download Node.js
• The official Node.js website has installation instructions for Node.js:
https://nodejs.org
FIRST APP:
Create a Node.js file named “hello.js", and add the following code:

var http = require('http');

http.createServer(function (req, res) {


res.writeHead(200,
{'Content-Type': 'text/html'});
res.end('Hello World!’);
}).listen(8080);

TO RUN:
TERMINAL : node [filename]
BROWSER : http://127.0.0.1:8080/
FIRST APP EXPLANATION:
The Built-in HTTP Module

Node.js has a built-in module called HTTP,


which allows Node.js to transfer data over the
Hyper Text Transfer Protocol (HTTP).

To include the HTTP module, use the require()


method:

var http = require('http');


FIRST APP EXPLANATION:
Node.js as a Web Server :
The HTTP module can create an HTTP
server that listens to server ports and gives
a response back to the client.

Use the createServer() method to create an


HTTP server.
The function passed into the
http.createServer() method, will be
executed when someone tries to access
the computer on port 8080.
FIRST APP EXPLANATION:
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:
res.writeHead(200, {'Content-Type': 'text/html’});

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.

The function passed into the http.createServer() has a


req argument that represents the request from the
client, as an object (http.IncomingMessage object).
ASYNCHRONOUS MODEL:
Node.js uses asynchronous programming!

A common task for a web server can be to open a file on the


server and return the content to the client.

• Here is how Node.js handles a file request:


1.Sends the task to the computer's file system.
2.Ready to handle the next request.
3.When the file system has opened and read the file, the
server returns the content to the client.
• Node.js eliminates the waiting, and simply continues with
the next request.
• Node.js runs single-threaded, non-blocking, asynchronous
programming, which is very memory efficient.
NODEJS FILE SYSTEM:
The Node.js file system module allows you to
work with the file system on your computer.
To include the File System module, use the
require() method:
var fs = require('fs');

Common use for the File System module:


Read files Delete files
Create files Rename files
Update files

The fs.readFile() method is used to read files


on your computer.
NODEJS FILE SYSTEM:
Create Files
The File System module has methods for creating new
files:

fs.appendFile()
fs.open()
fs.writeFile()

The fs.appendFile() method appends specified content


to a file. If the file does not exist, the file will be created.
NODEJS FILE SYSTEM:
The fs.writeFile() method replaces the
specified file and content if it exists. If the
file does not exist, a new file, containing the
specified content, will be created.

The File System module has methods for updating


files:
fs.appendFile()
fs.writeFile()
The fs.appendFile() method appends the
specified content at the end of the specified
file.
NODEJS FILE SYSTEM:
To delete a file with the File System module,
use the fs.unlink() method.

The fs.unlink() method deletes the specified


file.

To rename a file with the File System module,


use the fs.rename() method.

The fs.rename() method renames the specified


file.
CALLBACKS:
A callback is a function that is called when a
task is completed, thus helping in preventing
any kind of blocking and a callback function
allows other code to run in the meantime.
Callback is called when a task gets completed
and is the asynchronous equivalent of a
function.
EVENT LOOP:
• An event loop is an endless loop, which waits for tasks,
executes them, and then sleeps until it receives more
tasks.
• The event loop executes tasks from the event queue
only when the call stack is empty i.e. there is no
ongoing task.
• The event loop allows us to use callbacks and
promises.
• The event loop executes the tasks starting from the
oldest first.
REPL:

REPL stands for Read Evaluate Print


Loop, and it is a programming language
environment (basically a console
window) that takes single expression as
user input and returns the result back
to the console after execution.
The REPL session provides a convenient
way to quickly test simple JavaScript
code and nodejs code.
NODEJS EVENT-EMITTER:
Node.js uses events module to create and handle
custom events.
The EventEmitter class can be used to create
and handle custom events module.
The syntax to Import the events module are
given below:
const EventEmitter = require('events’);

This object exposes, the on and emit methods.


Emit = is used to trigger an event
On = is used to add a callback function that's
going to be executed when the event is
triggered
NETWORKING MODULE:
Node.js has a ‘net’ module which provides an
asynchronous network API for creating stream-
based TCP/IPC servers and clients.
It can be accessed using:
var net = require('net');

To create a TCP/IPC based server, we use the


createServer method.
var server = net.createServer();

Refer :
https://www.tutorialspoint.com/nodejs/nodejs_ne
t_module.htm
NODEJS BUFFERS:
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.

Syntax:
The syntax for creating an empty Buffer of the
length 15:
var buf = Buffer.alloc(15);

alloc() Creates a Buffer object of the


specified length.
NODEJS STREAMS:
A stream is a sequence of data that is being
moved from one point to another over time.
Streams are objects that let you read data from
a source or write data to a destination in
continuous fashion.
In Node.js, there are four types of streams:

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.
NODEJS STREAMS:
Each type of Stream is an EventEmitter instance
and throws several events at different instance
of times. For example, some of the commonly
used events are −

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

You might also like