|
| 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 | + |
0 commit comments