-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsread.js
53 lines (29 loc) · 862 Bytes
/
fsread.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/* read file using fs module
Program -1
var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
fs.readFile('demofile1.html', function(err, data) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
return res.end();
});
}).listen(8087);
*/
////////////////////////////////////////////////////////////
/* program -2 Read file using flag */
var http = require('http');
var fs = require('fs');
http.createServer((req, res)=> {
console.log("server created");
var data = Buffer.from('myNewFile.txt', 'utf-8');
fs.open(data, 'r', (err, data)=> {
if (err) {
return console.error(err);
}
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write(data);
//console.log("file opened");
return res.end();
});
}).listen(8087);