-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
387 lines (188 loc) · 7.04 KB
/
index.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// const superHero = require("./super-hero");
// //REQUIRE OR IMPORT FILES
// const math = require("./math.js")
// console.log(math.add(3, 5))
// console.log(math.subtract(3, 5))
// //IMPORTING JSON
// const data = require("./data.json");
// console.log(data);
// console.log(data.name)
// //adding (node --watch) to the terminal restarts after changes
// //without the braces just type node --watch and run the codenode index
// function greet(name) {clearInterval
// console.log(`Hello ${name}`)
// }
// //here, the function receives a function as a parameter
// function greetVishwas(greetFn) {
// const name = "Vishwas";
// greetFn(name)
// }
// greetVishwas(greet)
// //event module(registers events listeners using on method)
// const EventEmitter = require("node:events");
// const emitter = new EventEmitter();
// emitter.on("order-pizza", () => {
// console.log("order received! Baking started")
// });
// //using an emit method to emit an event
// emitter.emit("order-pizza");
// //BUFFER
// const buffer = new Buffer.from("evans");
// console.log(buffer.toJSON());
// console.log(buffer.toString())
// //WORKING WITH FS MODULE
// const fs = require("node:fs");
// // using readFilesync method of fs to acces fies
// const fileContent = fs.readFileSync("./file.txt");
// //convert to string since it returns binary data in buffer mode
// console.log(fileContent.toString());
// //read file method to do it asynchronously
// //utf-8 tells it to encode the binary data to normal
// fs.readFile("./file.txt", "utf-8",(error, data) => {
// if (error) {
// console.log(error)
// } else {
// console.log(data)
// }
// });
// //writeFileSync method
// //the code below will create a new file with the text in it
// fs.writeFileSync("./greet.txt", "Hello new file")
// fs.writeFile("./greet.txt", " Hello async write", { flag: "a" },
// (err) => {
// if (err) { console.log(err); }
// else{console.log("file written")}
// }
// )
// //USING FS/PROMISES
// const fs = require("fs").promises;
// fs.readFile("file.txt", "utf-8")
// .then((data) => console.log(data))
// .catch(err=>console.log(err))
// //USING FS STREAMS
// const fs = require('fs');
// const readableStream = fs.createReadStream("./file.txt",
// {
// encoding: "utf-8",
// highWaterMark: 2,//meaning stream 2 bytes or xtrs per time
// }
// );
// //writing and creating a new file with file.txt content
// const writeableStream = fs.createWriteStream("./file2.txt");
// //using write method to append content from readable stream(file.txt)
// readableStream.on("data", (chunk) => {
// console.log(chunk);
// writeableStream.write(chunk);
// });
// //PIPES
// const fs = require('fs');
// const readableStream = fs.createReadStream("./file.txt",
// {
// encoding: "utf-8",
// highWaterMark: 2,//meaning stream 2 bytes or xtrs per time
// }
// );
// //writing and creating a new file with file.txt content
// const writeableStream = fs.createWriteStream("./file2.txt");
// //now we can directly pipe writeable steam uisong .pipe method
// //it pipes data from file.txt to file2.txt created above
// readableStream.pipe(writeableStream);
// //USING HTTP MODULE AND CREATING A SERVER
// const http = require("http");
// //using the createServer method of http
// const server = http.createServer((req, res) => {
// //method write with http status code
// res.writeHead(200);
// res.end("Hello http");
// });
// //using the listen method on server & specifying ports
// server.listen(3000, () => {
// console.log("Server listening on port 3000")
// });
// //RESPONDING WITH JSON
// const http = require("http");
// const server = http.createServer((req, res) => {
// //method write with http status code
// const superHero = {
// firstname: "Evans",
// lastName: "Pixels",
// }
// res.writeHead(200);
// //convert using json stringify since plain objects are not supported
// res.end(JSON.stringify(superHero));
// });
// //using the listen method on server & specifying ports
// server.listen(3000, () => {
// console.log("Server listening on port 3000")
// });
// //RESPONDING WITH HTML FILES
// const http = require("http");
// //require fs which reads files eg html
// const fs = require("fs")
// const server = http.createServer((req, res) => {
// //method write with http status code
// res.writeHead(200,{"Content-type":"text/html"});
// //using fs to read html file and responding it with res,end
// // const html = fs.readFileSync("./index.html","utf-8")
// // res.end(html);
// //alternatively and better way
// fs.createReadStream(__dirname + "./index.html").pipe(res)
// });
// //using the listen method on server & specifying ports
// server.listen(3000, () => {
// console.log("Server listening on port 3000")
// });
// //INJECTING DYNAMICS TO THE HTML RESPONDED using simple string replacement
// const http = require("http");
// const fs = require("fs")
// const server = http.createServer((req, res) => {
// res.writeHead(200, { "Content-type": "text/html" });
// //this variable below will be used in the html
// const name = "Memba"
// let html = fs.readFileSync("./index.html", "utf-8")
// //replacing the name in the html file to our variable
// html = html.replace("{{name}}", name)
// res.end(html);
// });
// //using the listen method on server & specifying ports
// server.listen(3000, () => {
// console.log("Server listening on port 3000")
// });
// //ROUTING WITH HTTP MODULE
// const http = require("http");
// const fs = require("fs")
// const server = http.createServer((req, res) => {
// //examples of how req are handled
// if (req.url === "/") {
// res.end("Home page")
// } else if (req.url === "/about") {
// res.end(JSON.stringify({
// name: "Evans",
// lastName: "Memba"
// }))
// } else {
// res.end("page doesnt exist")
// }
// });
// //using the listen method on server & specifying ports
// server.listen(3000, () => {
// console.log("Server listening on port 3000")
// });
//EVENT LOOPS.(TO-DO)
// NPM
//To test if you have npm installed, run (npm -v) in ur terminal
//to publish an npm package, run (npm adduser pixelvans) on your commaand
//PUBLISH
//npm publish on your command
//to see it run( https://www.npmjs.com/package/<package name> ) in your browser
//HOW TO DEAL WITH TERMINAL REFUSING SCRIPTS
// open POWERSHELL
//type: Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
// enter
// choose Y and enter
//close powershell and re open
//now run your package eg nodemon new.js
//BUILDING CLI TOOLS
//process.argv is an array containing the command-line arguments passed when the Node.js
//npm install yargs
// yargs is a powerful library for Node.js that simplifies the process of parsing and handling command-line arguments