-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
347 lines (287 loc) · 13.7 KB
/
app.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
/*
//======================================================================================================================================================================
const tutorial = require('./tutorial') // "./" artinya same path + nama file
// console.log(tutorial);
console.log(tutorial.sum(1,1));
console.log(tutorial.PI);
console.log(new tutorial.SomeMathObject);
//======================================================================================================================================================================
*/
/*
//======================================================================================================================================================================
//events module
const EventEmitter = require('events');
const eventEmitter = new EventEmitter();
eventEmitter.on('tutorial',(num1,num2) =>{ //jalankan fungsi berikut saat event tutorial dipangggil
console.log(num1+num2);
})
eventEmitter.emit('tutorial',1,2);//memanggil event tutorial, parameternya ('nama event',parameter1 func, par2 funct, so on)
class Person extends EventEmitter {
constructor(name){
super();
this._name = name;
}
get name(){
return this._name;
}
}
let aan = new Person('aan');
let apang = new Person('apang');
aan.on('name', ()=>{
console.log('my name is ' + aan.name);
})
apang.on('name', ()=>{
console.log('nama ane ' + apang.name);
})
aan.emit('name');
apang.emit('name');
//======================================================================================================================================================================
*/
/*
//======================================================================================================================================================================
//readline module
const readline = require('readline');
const rl = readline.createInterface({input : process.stdin, //proccess itu global object jadi ngak butuh require
output : process.stdout}) //take an object (configuration)
let num1 = Math.floor((Math.random()*10) + 1);
let num2 = Math.floor((Math.random()*10) + 1);
let answer = num1+num2;
rl.question(`What is ${num1} + ${num2} ?\n`,
(userInput)=>{
// console.log(userInput);
if(userInput.trim() == answer) {
rl.close(); //close the app when done
}
else {
rl.setPrompt('Incorrect response, please try again \n');
rl.prompt();
rl.on('line',(userInput) =>{
if(userInput.trim() == answer) {
rl.close(); //close the app when done
}else {
rl.setPrompt(`Your answer of ${userInput} is incorrect, try again \n`);
rl.prompt();
}
})
}
}) //parameter1 : string, parameter2 : function
rl.on('close', ()=> {
console.log('Correct!');
})
//======================================================================================================================================================================
*/
/*
//======================================================================================================================================================================
file system (fs) module - part 1
const fs = require('fs');
//create a file
// fs.writeFile('example.txt','this is an example', (err) => {
// if(err)
// console.log(err);
// else{
// console.log('file successfully created')
// fs.readFile('example.txt','utf8',(err,file)=>{ //read file
// if(err)
// console.log(err);
// else
// console.log(file) //output to binary
// })//(nama_file,ENCODING_TYPE, callback(err,file)) //kalau ngak masukin encoding type, keluarnya binary
// }
// })//(name_of_file, what_u_want_to_write_into_the_file, callback(mostly error))
//rename a file
// fs.rename('example.txt','example2.txt',(err)=>{
// if(err)
// console.log(err);
// else
// console.log('successfully renamed the file');
// }) //argument:('file_name+extension', 'rename_to,callback(err))
//append data to file
// fs.appendFile('example2.txt', '\nSome data being appended',(err)=>{
// if(err)
// console.log(err);
// else
// console.log('Data Added!');
// })//(namaFile,data yang mau ditambahkan,callback(err))
//delete a file
fs.unlink('example2.txt',(err)=>{
if(err)
console.log(err)
else
console.log('successfully deleted')
}) //(namafile,callback(err))
file system (fs) module - part 2
const fs = require('fs');
// function bikinFolder(namaFolder) {
// }
// fs.mkdir('tutorial', (err) =>{
// if(err)
// console.log(err)
// else{
// console.log('folder created');
// // fs.rmdir('tutorial', (err)=>{
// // if(err)
// // console.log(err)
// // else
// // console.log('successfully deleted the folder')
// // }) //argument (namafolder,callback)
// //create file inside the folder
// fs.writeFile('./tutorial/test1.txt', 'this is a test file', (err)=>{
// if(err)
// console.log(err);
// else
// console.log('file created');
// })
// }
// })// (namaFolder, callback(err))
// bikinFolder('tutorial');
// fs.unlink('./tutorial/test1.txt',(err)=>{
// if(err)
// console.log(err)
// else
// console.log('file successfully deleted')
// fs.rmdir('tutorial', (err) => {
// if(err)
// console.log(err);
// else
// console.log('folder deleted');
// })
// }) //(namafile,callback(err))
fs.readdir('./example',(err,files)=>{ //(directory name, callback(err, array:files))
if(err)
console.log(err)
else{
// console.log(files);
files.forEach(file =>{
fs.unlink(`./example/${file}`,(err)=>{
if(err)
console.log(err);
else
console.log(`file ${file} was deleted`);
})
})
}
})
//======================================================================================================================================================================
*/
/*
//======================================================================================================================================================================
Readable and WriteAble Stream
const fs = require('fs');
const readStream = fs.createReadStream('./example.txt', 'utf8'); //(filename,encode type)
const writeStream = fs.createWriteStream('example2.txt');
readStream.on('data',(chunk) => { //readsteam can read largefile, while fs.readfile cannot
// console.log(chunk);
writeStream.write(chunk);
})
//======================================================================================================================================================================
*/
/*
//======================================================================================================================================================================
Pipes and Pipe Chaining
const fs = require('fs');
const zlib = require('zlib'); //basically a module for compression (compressing file)
// const gzip = zlib.createGzip();
const gunzip = zlib.createGunzip();
// const readStream = fs.createReadStream('./example.txt', 'utf8'); //(filename,encode type)
// const writeStream = fs.createWriteStream('example2.txt.gz');
const readStream = fs.createReadStream('./example2.txt.gz'); //(filename,encode type)
const writeStream = fs.createWriteStream('uncompressed.txt');
readStream.pipe(gzip).pipe(writeStream)
readStream.pipe(gunzip).pipe(writeStream)// when using pipe, we need source stream and destination stream (in this case read and write stream)
//======================================================================================================================================================================
*/
/*
//======================================================================================================================================================================
create an Http Wrver Using the Http Module\
(basically create a web server with NodeJs)
const http = require('http');
const server = http.createServer((req,res)=>{
if(req.url === '/'){
res.write('Hello World from nodejs');
res.end(); //send the response back to client
}else {
res.write('using some other domain')
res.end();
}
}); // take callback (req,res)
server.listen('3000');
//======================================================================================================================================================================
*/
/*
//======================================================================================================================================================================
Serving Static Files with Http and File System Modules
const http = require('http');
const fs = require('fs');
http.createServer((req,res) => {
const readStream = fs.createReadStream('./static/example.png');
res.writeHead(200,{'Content-type' : 'image/png'});
readStream.pipe(res);
}).listen('3000');
//======================================================================================================================================================================
*/
/*
//======================================================================================================================================================================
installing packages using npm
const _ = require('lodash');
let example = _.fill([1,2,3,4,5], "watermelon",1,4);
console.log(example)
//======================================================================================================================================================================
*/
/*
//======================================================================================================================================================================
Semantic Versioning
(basically a standard that allow node js follows)
^ = for 4.x.x
~ = for 4.17.x
gak pakai = gak usah update
//======================================================================================================================================================================
*/
/*
//======================================================================================================================================================================
Getting started with Express Web Framework
const express = require('express');
const app = express(); //basically this is a function that return object than return a buch of method that can be used in our app
app.get('/',(req,res)=>{
res.send('Hello World');
}); //method get, argument (route,callback(req,res))
app.listen(3000);
//======================================================================================================================================================================
*/
/*
//======================================================================================================================================================================
Working with Express Get Requests
const express = require('express');
const app = express(); //basically this is a function that return object than return a buch of method that can be used in our app
app.get('/',(req,res)=>{
res.send('Hello World');
}); //method get, argument (route,callback(req,res))
app.get('/example',(req,res)=>{
res.send('This is example route');
});
app.get('/example/:name/:age',(req,res)=>{
console.log(req.params); //output object based on what we input in url, use this for fixed parameter
console.log(req.query); //use this for optinal parameter, such as when we want what sord data by "age"
// res.send('example with route params');
res.send(req.params.name + ' : ' + req.params.age); //this variable from :name and :age of the req(request)
});
app.listen(3000);
//======================================================================================================================================================================
*/
/*
//======================================================================================================================================================================
Serving Static Files With Express
(HTML file, css, client-side js, images, videos)
const express = require('express');
const path = require('path');
const app = express();
app.use('/public', express.static(path.join(__dirname,'static'))); //meaning using middleware arguments:(alias_for_folder, express.method) ->alias is giving an alias to the file, in here alias to static folder, __dirname is return string where this file located
app.get('/',(req,res) => {
res.sendFile(path.join(__dirname,'static','index.html')) //arguments (dir,folderName,fileName)
}).listen(3000);
//======================================================================================================================================================================
*/
/*
//======================================================================================================================================================================
Http Post Request w/ Express and Body Parser Module
//======================================================================================================================================================================
*/