Skip to content

Commit fe4df56

Browse files
authored
Merge pull request #2 from XenoCod/Aditya_Kumar_Singh
Day 3 | NodeJs File Operations
2 parents 4b5f8ef + ed145b2 commit fe4df56

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

Day3/Day3.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// //Creating the server
2+
// var http= require('http');
3+
4+
// http.createServer(function(req, res){
5+
// res.writeHead(200, {'Content-Type': 'text/plain'});
6+
// res.write('This is the server home page.');
7+
// res.end();
8+
// }).listen(9000);
9+
10+
11+
//Reading the html file
12+
var http = require('http');
13+
var fs= require('fs');
14+
15+
http.createServer(function(req, res){
16+
fs.readFile('index.html', function(err,data){
17+
res.writeHead(200, {'Content-Type': 'text/html'});
18+
res.write(data);
19+
return res.end();
20+
})
21+
22+
}).listen(9000);
23+
24+
25+
function appendFile(){
26+
fs.appendFile('demo.txt', 'This is a demo file', function (err) {
27+
if (err) throw err;
28+
console.log('Saved!');
29+
});
30+
}
31+
32+
//Append to file
33+
appendFile();
34+
35+
//Opening the file
36+
function openFile(){
37+
fs.open('demo.txt', 'w', function (err, file) {
38+
if (err) throw err;
39+
console.log('Saved!');
40+
});
41+
};
42+
43+
openFile();
44+
45+
46+
//Overwriting the present context
47+
function overwrite(){
48+
fs.writeFile('demo.txt', 'Overwrittern', function (err) {
49+
if (err) throw err;
50+
console.log('Saved!');
51+
});
52+
};
53+
54+
// overwrite();
55+
56+
57+
//Updating the file present
58+
59+
function updateFile(){
60+
fs.appendFile('demo.txt', 'Updating the content.', function (err) {
61+
if (err) throw err;
62+
console.log('Updated!');
63+
});
64+
}
65+
66+
updateFile();
67+
68+
69+
//Deleting the specified file
70+
function deleteFile(){
71+
fs.unlink('demo.txt', function (err) {
72+
if (err) throw err;
73+
console.log('File deleted!');
74+
});
75+
};
76+
77+
// deleteFile();
78+
79+
//Renaming the presnt File
80+
function rename(){
81+
fs.rename('demo.txt', 'renamedDemo.txt', function (err) {
82+
if (err) throw err;
83+
console.log('File Renamed!');
84+
});
85+
}
86+
rename();
87+
88+

Day3/index.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Day3</title>
8+
</head>
9+
<body>
10+
<div>
11+
<h1>This is a index page which is running on server.</h1>
12+
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Doloribus, repellendus!</p>
13+
</div>
14+
</body>
15+
</html>

Day3/renamedDemo.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a demo fileUpdating the content.

0 commit comments

Comments
 (0)