forked from kat4/wordkat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordkat.js
94 lines (83 loc) · 3.12 KB
/
wordkat.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
var wordkat = (function() {
var http = require('http');
var fs = require('fs');
var PORT = 8000;
var index = fs.readFileSync(__dirname + '/index.html');
var frontendtests = fs.readFileSync(__dirname + '/tests/frontendtests.html');
var frontendtestsjs = fs.readFileSync(__dirname + '/tests/frontendtests.js');
var acjs = fs.readFileSync(__dirname + '/js/ac.js');
var wnjs = fs.readFileSync(__dirname + '/js/wn.js');
var wordlist = fs.readFileSync(__dirname + '/text/words.txt').toString().split('\n');
var autocomplete = require('./autocomplete.js');
var wordnik = require('./wordnik.js');
function handler(request, response) {
var searchTerm = '';
var url = request.url;
var urlArr = request.url.split('/');
console.log('new request from', urlArr);
if (urlArr[1] === ('success')) {
response.end('success');
} else if (urlArr[1] === ('frontendtests.js')) {
response.writeHead(200, {
"Content-Type": "text/javascript"
});
response.end(frontendtestsjs);
} else if (urlArr[1] === ('test')) {
response.writeHead(200, {
"Content-Type": "text/html"
});
response.end(frontendtests);
} else if (urlArr[1] === ('search')) {
searchTerm = urlArr[2];
var acResponse = JSON.stringify(autocomplete.filter_word(searchTerm, wordlist).slice(0, 19));
response.write(acResponse);
response.end(); //autocomplete());
} else if (urlArr[1] === ('worddef')) {
searchTerm = urlArr[2];
wordnik(searchTerm, function(data) {
response.write(data);
response.end();
});
} else if (urlArr[1] === ('js')) {
if (urlArr[2] === ('ac.js')) {
response.writeHead(200, {
"Content-Type": "text/html"
});
response.end(acjs);
}
if (urlArr[2] === ('wn.js')) {
response.writeHead(200, {
"Content-Type": "text/html"
});
response.end(wnjs);
}
} else if (urlArr.length === 1 || urlArr[1] === '') {
response.writeHead(200, {
"Content-Type": "text/html"
});
response.end(index);
} else {
fs.readFile(__dirname + request.url, function(err, file) {
if (err) {
response.end();
} else {
var ext = request.url.split('.')[1];
response.writeHead(200, {
'Content-Type': 'text/' + ext
});
}
response.end(file);
});
}
}
function startServer() {
var server = http.createServer(handler);
server.listen(process.env.PORT || PORT);
console.log("to see this locally got to port 8000");
}
return {
handler: handler,
startServer: startServer
};
}());
module.exports = wordkat;