Node.js Callbacks and Events: Understanding I/O and Event-Driven Programming, Slides of Web Programming and Technologies

An introduction to Node.js callbacks and events, explaining how they are used for non-blocking I/O and event-driven programming. Topics covered include the HTTP and File System modules, reading and creating files, and the event loop and EventEmitter class.

Typology: Slides

2020/2021

Uploaded on 05/16/2021

hirdesh-kumar-1
hirdesh-kumar-1 🇵🇰

1 / 42

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CALLBACKS, EVENTS Dr. Adeel Ansari
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a

Partial preview of the text

Download Node.js Callbacks and Events: Understanding I/O and Event-Driven Programming and more Slides Web Programming and Technologies in PDF only on Docsity!

CALLBACKS, EVENTS

Dr. Adeel Ansari

TOPICS TO BE COVERED

HTTP Module

FS Module

OS Module

URL Module

Event Loops and Event Emitter Class

BLOCKING CODE EXAMPLE

Create a text file named input.txt with some text content.

Create a main.js file with the following code:

var fs = require("fs");

var data =

fs.readFileSync('input.txt');

console.log(data.toString());

console.log("Program Ended");

NON-BLOCKING CODE

EXAMPLE

Create a text file named input.txt with some text content.

Create a main.js file with the following code:

var fs = require("fs");

fs.readFile('input.txt', function (err, data) {

if (err) return console.error(err);

console.log(data.toString());

console.log("Program Ended");

HTTP MODULE

Dr. Adeel Ansari

RECALL THE HTTP CODE

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

argument that represents the request from the client, as an object

(http.IncomingMessage object).

This object has a property called "url" which holds the part of the

url that comes after the domain name:

var http = require('http');

http.createServer(function (req, res) {

res.writeHead( 200 , {'Content-Type': 'text/html'

res.write('Hello World!');

res.end();

}).listen( 8080 );

Only “/” gets

printed on the

webpage.

FS MODULE

Dr. Adeel Ansari

NODE.JS AS A FILE SERVER

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

Create files

Update files

Delete files

Rename files

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

CREATE A NEW FILE USING

THE APPENDFILE() METHOD:

var fs = require('fs');

fs.appendFile('mynewfile1.txt', 'Hello

Content!', function (err) {

if (err) throw err;

console.log('Saved!');

CREATE A NEW FILE USING

THE WRITEFILE() METHOD:

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:

var fs = require('fs');

fs.writeFile('mynewfile3.txt', 'Hello

content!', function (err) {

if (err) throw err;

console.log('Saved!');

UPDATE FILES

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:

var fs = require('fs');

fs.appendFile('mynewfile1.txt', ' This is my

text.', function (err) {

if (err) throw err;

console.log('Updated!');

DELETE FILES

To delete a file with the File System module, use the fs.unlink()

method.

The fs.unlink() method deletes the specified file:

var fs = require('fs');

fs.unlink('mynewfile2.txt', function (err) {

if (err) throw err;

console.log('File deleted!');

RENAME FILES

To rename a file with the File System module, use the fs.rename()

method.

The fs.rename() method renames the specified file:

var fs = require('fs');

fs.rename('mynewfile1.txt', 'myrenamedfile.txt', function (err

if (err) throw err;

console.log('File Renamed!');