-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_write_file.js
More file actions
144 lines (119 loc) · 3.74 KB
/
Copy pathread_write_file.js
File metadata and controls
144 lines (119 loc) · 3.74 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// run `node index.js` in the terminal
console.log(`Hello Node.js v${process.versions.node}!`);
// const command = process.argv[2]
// if (command === 'add') {
// console.log('Adding note!')
// } else if (command === 'remove') {
// console.log('Removing note!')
// }
// import fs module in which writeFile, readFile function is defined.
const fsLibrary = require('fs')
// Data which will need to add in a file.
// let data = "Hello world."
// Write data in 'newfile.txt' .
// fsLibrary.writeFile('newfile.txt', data, (error) => {
// // In case of a error throw err exception.
// if (error) throw err;
// })
// Reading data in utf-8 format which is a type of character set.
// Instead of 'utf-8' it can be other character set also like 'ascii'
// fsLibrary.readFile('test.txt', 'utf-8', (err, data) => {
// if (err) throw err;
// // Converting Raw Buffer to text
// // data using tostring function.
// console.log(data.toString());
// })
// function createFile(){
// var object = new ActiveXObject("Scripting.FileSystemObject");
// var file = object.CreateTextFile("indxcd.txt", false);
// file.WriteLine('Hello World');
// file.WriteLine('Hope is a thing with feathers, that perches on the soul.');
// file.Close();
// }
// To open a new file-
// let newfile = new ActiveXObject("Scripting.FileSystemObject");
// let openFile = newfile.OpenTextFile("C:\\testfile.txt", 1, true);
// To write data to a file-
// var editFile = newfile.CreateTextFile("c:\\Demofile.txt", true);
// var editFile = newfile.CreateTextFile("Demofile.txt", true);
// editFile.WriteLine("Add sample text to the file...");
// editFile.WriteLine('steadyAdvice');
// editFile.Close();
// Fetch any file(images, text file) URL-
var http = require('http');
var fs = require('fs');
var url = require('url');
// Create a server
http.createServer( function (request, response) {
// Parse the request containing file name
var pathname = url.parse(request.url).pathname;
// console.log(url.parse(request.url))
// Print the name of the file for which request is made.
console.log("Request for " + pathname + " received.");
// Read the requested file content from file system
// Non-Blocking Code Example-
fs.readFile(pathname.substr(1), function (err, data) {
if (err) {
console.log(err);
// HTTP Status: 404 : NOT FOUND
// Content Type: text/plain
response.writeHead(404, {'Content-Type': 'text/html'});
return;
}else {
//Page found
// HTTP Status: 200 : OK
// Content Type: text/plain
response.writeHead(200, {'Content-Type': 'text/html'});
// Write the content of the file to response body
response.write(data.toString());
}
// Send the response body
response.end();
});
}).listen(8081);
// console.log('Server running at http://127.0.0.1:8081/');
// var readData = fs.readFileSync('test.txt');
// console.log(readData.toString());
// console.log("Program Ended");
// const yargs = require('yargs')
// yargs.version('1.1.0')
// yargs.command({
// command: 'add',
// describe: 'Add a new note',
// handler: function () {
// console.log('Adding a new note!')
// }
// })
// console.log(yargs.argv)
// yargs.command({
// command: 'add',
// describe: 'Add a new note',
// builder: {
// title: {
// describe: 'Note title',
// demandOption: true,
// type: 'string'
// },
// body: {
// describe: 'Note body',
// demandOption: true,
// type: 'string'
// }
// },
// handler: function (argv) {
// console.log('Title: ' + argv.title)
// console.log('Body: ' + argv.body)
// }
// })
const geocode = (address, callback) => {
setTimeout(() => {
const data = {
latitude: 0,
longitude: 0
}
callback(data)
}, 2000)
}
geocode('Philadelphia', (data) => {
console.log(data)
})