-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
163 lines (133 loc) · 3.76 KB
/
test.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
// const greet = (name) => {
// console.log(`hello, ${name}`);
// }
// greet ("Jamie");
// greet ("Arodi");
// console.log(global);
// setTimeout(() => {
// console.log("Hello there after all this time");
// clearInterval(int);
// }, 3000);
// const int = setInterval(() => {
// console.log("another interval");
// }, 100);
// console.log(__dirname);
// console.log(__filename);
// const people = ["yoshi", "Amerix", "Maya"];
// const ages = [20, 30, 40];
// module.exports = {
// people,
// ages};
// console.log("test logged");
//file system
//reading files
// const fs = require('fs');
// const { Server } = require('http');
// fs.readFile('./docs/blog1.txt', (err, data)=>{
// if(err){
// console.log(err);
// }
// console.log(data.toString());
// });
// console.log("last line of code"); used to test for asynchronous
//writing files
// fs.writeFile('./docs/blog1.txt', "hello world", ()=>{
// console.log("file was written");
// });
// fs.writeFile('./docs/blog2.txt', "hello Jesus my old firend", ()=>{
// console.log("file was written");
// });
//directories
// if(!fs.existsSync('./assets')){
// fs.mkdir('./assets', (err) => {
// if (err){
// console.log (err);
// }
// console.log('folder created');
// });
// }
// else {
// fs.rmdir('./assets', (err) =>{
// if(err){
// console.log (err);
// }
// console.log('folder created');
// })
// }
//deleting files
// if (fs.existsSync('./docs/deleteme.txt')){
// fs.unlink('./docs/deleteme.txt', (err) =>{
// if (err) {
// console.log(err)
// }
// console.log('the file has been deleted');
// })
// }
//streams and buffers
// const readStream = fs.createReadStream('./docs/blog1.txt', { encoding: 'utf-8'});
// const writeStream = fs.createWriteStream('./docs/blog4.txt');
// read stream
// readStream.on('data', (chunk) => {
// console.log('...NEW CHUNK...');
// console.log(chunk);
// });
//write stream
// readStream.on('data', (chunk) =>{
// console.log('..NEW CHUNK..');
// console.log(chunk);
// writeStream.write('\nNEW CHUNK\n');
// writeStream.write(chunk);
// })
//piping
// readStream.pipe(writeStream);
//path module
// const path = require ('path');
// const pathObj = path.parse(__filename);
// console.log(pathObj);
// console.log(pathObj.name);
// http Server
//set header content type
// res.setHeader('Content-Type', 'text/html');
// res.write('<head><link rel = "stylesheet" href = "#"></link></head>');
// res.write('<p>hello my ninjas</p>');
// res.write('<p>hello my ninjas still ninjaing</p>');
// res.end();
//basic routing without express
// const server = http.createServer((req, res) =>{
// console.log(req.url, req.method);
// console.log('request logged');
// //set header content type
// res.setHeader('Content-Type', 'text/html');
// //creating the routes
// let path = './views/';
// switch(req.url){
// case '/':
// path += 'index.html';
// res.statusCode = 200;
// break;
// case '/about':
// path += 'about.html';
// res.statusCode = 200;
// break;
// case '/about-me':
// res.statusCode = 301;
// res.setHeader('Location', '/about');
// res.end();
// break;
// default:
// path += '404.html';
// res.statusCode = 404;
// break;
// }
// //writing an html file
// fs.readFile(path, (err, data) =>{
// if(err){
// console.log(err)
// res.end();
// }
// else{
// res.write(data);
// res.end();
// }
// });
// });