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!');