


































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 42
This page cannot be seen from the preview
Don't miss anything!



































Dr. Adeel Ansari
var fs = require("fs");
var data =
fs.readFileSync('input.txt');
console.log(data.toString());
console.log("Program Ended");
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");
Dr. Adeel Ansari
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.
Dr. Adeel Ansari
var fs = require('fs');
Common use for the File System module:
Read files
Create files
Update files
Delete files
Rename files
var fs = require('fs');
fs.appendFile('mynewfile1.txt', 'Hello
Content!', function (err) {
if (err) throw err;
console.log('Saved!');
var fs = require('fs');
fs.writeFile('mynewfile3.txt', 'Hello
content!', function (err) {
if (err) throw err;
console.log('Saved!');
var fs = require('fs');
fs.appendFile('mynewfile1.txt', ' This is my
text.', function (err) {
if (err) throw err;
console.log('Updated!');
var fs = require('fs');
fs.unlink('mynewfile2.txt', function (err) {
if (err) throw err;
console.log('File deleted!');
var fs = require('fs');
fs.rename('mynewfile1.txt', 'myrenamedfile.txt', function (err
if (err) throw err;
console.log('File Renamed!');