-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitsearch.js
More file actions
222 lines (184 loc) · 5.48 KB
/
Copy pathgitsearch.js
File metadata and controls
222 lines (184 loc) · 5.48 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
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
// HELLO WORLD
// console.log("HELLO WORLD");
// BABY STEPS
// add the numbers passed as arguments and return the sum of them
// note: it starts at index 2 as the first two array values are 'node' and then the path to your program
// var sum = 0;
// for(var i = 2; i <= process.argv.length -1; i++){
// sum += +process.argv[i];
// }
// console.log(sum);
// I/O
// read a file and print the number of lines it contains
// var fs = require('fs');
// var buffer = fs.readFileSync(process.argv[2]);
// var lines = buffer.toString().split('\n').length - 1;
// console.log(lines);
// note: you can avoid the .toString() by passing 'utf8' as the
// second argument to readFileSync, then you'll get a String!
//
// fs.readFileSync(process.argv[2], 'utf8').split('\n').length - 1
// ASYNC I/O
// read a file asynchronously and print the number of lines it contains
// var fs = require('fs');
// var file = process.argv[2];
// fs.readFile(file, 'utf8', function(err, data){
// if(err) throw err;
// //console.log(data);
// var lines = data.split('\n').length - 1;
// console.log(lines);
// });
// FILTERED LS
// create a program that prints a list of files in a given directory,
// filtered by the extension of the files.
// var fs = require('fs');
// var path = require('path');
// var dir = process.argv[2];
// var ext = process.argv[3];
// fs.readdir(dir, function(err, list) {
// if(err) throw err;
// for (var i = 0; i < list.length; i++) {
// if(path.extname(list[i]) == "."+ext)
// console.log(list[i]);
// };
// });
// MODULAR (functions)
// var dir = process.argv[2];
// var ext = process.argv[3];
// var mod = require('./dirextmodule');
// mod(dir, ext, function(err, data){
// if(err) return console.error('There was an error:', err);
// data.forEach(function (file){
// console.log(file);
// })
// });
// HTTP CLIENT
// Write a program that performs HTTP GET requests... and it
// reads the response for data, error and end.
// var http = require('http');
// http.get(process.argv[2], function(response){
// response.setEncoding('utf8');
// response.on('data', console.log);
// response.on('error', console.error);
// });
// HTTP COLLECT
//
// var bl = require('bl');
// var http = require('http');
// http.get(process.argv[2], function(response){
// response.pipe(bl(function(err, data){
// if(err)
// return console.error(err);
// console.log(data.length);
// console.log(data.toString());
// }))
// });
// ASYNC
// note: try official solution using a counter
// var http = require('http');
// var bl = require('bl');
// var url1 = process.argv[2];
// var url2 = process.argv[3];
// var url3 = process.argv[4];
// http.get(url1, function(response){
// response.pipe(bl(function(err, data){
// if(err)
// return console.error(err);
// if(data)
// {
// console.log(data.toString());
// http.get(url2, function(response){
// response.pipe(bl(function(err, data){
// if(err)
// return console.error(err);
// if(data){
// console.log(data.toString());
// http.get(url3, function(response){
// response.pipe(bl(function(err, data){
// if(err)
// return console.error(err);
// if(data)
// console.log(data.toString());
// }))
// });
// }
// }))
// });
// }
// }))
// });
// TIME SERVER
// var net = require('net');
// function zeroFill(i){
// return (i < 10 ? '0' : '') + i
// }
// function now(){
// var d = new Date()
// return d.getFullYear() + '-'
// + zeroFill(d.getMonth() + 1) + '-'
// + zeroFill(d.getDate()) + ' '
// + zeroFill(d.getHours()) + ':'
// + zeroFill(d.getMinutes())
// }
// var server = net.createServer(function (socket){
// socket.end(now() + '\n')
// })
// server.listen(Number(process.argv[2]));
// HTTP FILE SERVER
// var http = require('http');
// var fs = require('fs');
// var server = http.createServer(function(request, response){
// var file = process.argv[3];
// var readStream = fs.createReadStream(file);
// readStream.on('open', function(){
// readStream.pipe(response);
// });
// readStream.on('error', function(err){
// response.end(err)
// })
// })
// server.listen(Number(process.argv[2]))
// HTTP UPPERCASERER
// var http = require('http');
// var fs = require('fs');
// var map = require('through2-map');
// var port = process.argv[2];
// var server = http.createServer(function(req, res){
// req.on('data', function(data){
// data = String(data).toUpperCase();
// res.write(data);
// res.setTimeout(1000, function(){
// res.end();
// });
// });
// });
// server.listen(Number(port));
// HTTP JSON API SERVER
var url = require('url');
var qs = require('querystring');
var http = require('http');
var server = http.createServer(function(req, res){
res.writeHead(200, { 'Content-Type': 'application/json' });
if(req.method === 'GET') {
var srvUrl = req.url;
var path = url.parse(srvUrl).pathname;
if(path === '/api/parsetime'){
var str = JSON.stringify(qs.parse(url.parse(srvUrl).query, true));
var date = new Date(str.substr(8, 19));
console.log(date);
res.write('{\"hour\":' + date.getHours() + ',\"minute\":'
+ str.substr(22,2) + ',\"second\":' + str.substr(25,2) + '}'
)
}
if(path === '/api/unixtime'){
var query = url.parse(req.url, true).query;
//console.log(query.iso);
//var str = JSON.stringify(query.iso);
var str = JSON.stringify(qs.parse(url.parse(srvUrl).query, true));
var date = new Date(str.substr(8,24));
res.write('{\"unixtime\":' + date.getTime() + '}')
}
res.end();
}
})
server.listen(process.argv[2]);