-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
53 lines (38 loc) · 1.37 KB
/
server.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
/**********************************************************
*/
// Node-specific requires
const express = require('express'),
bodyParser = require('body-parser'),
path = require('path'),
cors = require('cors');
const app = express();
// Middleware
app.use(bodyParser.urlencoded({'extended': false}));
app.use(bodyParser.json());
app.use(cors());
// Application specific requires
const config = require(path.join(__dirname, "./modules/config.js"));
const words = require(path.join(__dirname, "./modules/loadwords.js"));
// loadWords returns the loaded, sorted wordlist -- see
// the modules/loadwords.js file for more info.
//
const word_list = words.loadWords(config);
///////////////////////////////////////////////////////////
// Requests.
//
// There is only one route which is /api/ followed by
// the query string.
app.get("/api/:query", function(request, response) {
var query = request.params.query;
if (typeof(query) === "undefined" || query == "") {
response.send("!");
response.end();
}
else {
response.send(`${word_list.getMatch(query)}`);
response.end();
}
});
app.listen(config.APP_PORT, function() {
console.log(`Listening on port ${config.APP_PORT}`);
});