-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhandler.js
More file actions
65 lines (48 loc) · 1.46 KB
/
handler.js
File metadata and controls
65 lines (48 loc) · 1.46 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
var fs = require('fs');
var ac = require('./index.js');
var index = fs.readFileSync(__dirname + '/index.html');
module.exports = function handler(request, response) {
if (request.url.length === 1) {
response.writeHead(200, {
"Content-Type": "text/html"
});
response.end(index);
} else if (request.url.indexOf('/define') > -1) {
var userInput = request.url.split('/')[2].toString();
response.writeHead(200, {
"Content-Type": "text/html"
});
//response.write(userInput);
ac.import(function(err, words) {
ac.findWord(userInput, function(err, found) {
response.write(JSON.stringify(found));
var ran = Math.random();
if (found.length > 10) {
ran = Math.floor(ran * 10);
} else {
ran = Math.floor(ran * found.length);
}
ac.define(found[ran], function(err, definition) {
response.write('£' + definition);
response.end('£' + ran.toString());
});
});
});
} else {
fs.readFile(__dirname + request.url, function(err, file) {
if (err) {
// dont reply 200 currently replying with 200 if in this loop.
response.writeHead(404, {
'Content-Type': 'text/' + ext
});
response.end();
} else {
var ext = request.url.split('.')[1];
response.writeHead(200, {
'Content-Type': 'text/' + ext
});
response.end(file);
}
});
}
};