-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathserver.js
83 lines (64 loc) · 2.38 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
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
const express = require('express');
const app = express();
const friendlyWords = require('./index');
const sampleSize = require('lodash.samplesize');
// Host our static site, thereby providing our html, css, and js.
app.use(express.static('public'));
// CORS - Allow pages from any domain to make requests to our API
app.use(function(request, response, next) {
response.header("Access-Control-Allow-Origin", "*");
response.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
return next();
});
const sample = (words) => {
return sampleSize(words, 10);
}
const pairs = (firstWords, secondWords) => {
if(firstWords.length !== secondWords.length) {
console.error("Word pair collection lengths must match.");
return null;
}
const pairedWords = firstWords.map(
(firstWord, index) => (`${firstWord}-${secondWords[index]}`)
);
return pairedWords;
}
const triples = (firstWords, secondWords, thirdWords) => {
if(firstWords.length !== secondWords.length || firstWords.length !== thirdWords.length || secondWords.length !== thirdWords.length) {
console.error("Word pair collection lengths must match.");
return null;
}
const tripledWords = firstWords.map(
(firstWord, index) => (`${firstWord}-${secondWords[index]}-${thirdWords[index]}`)
);
return tripledWords;
}
app.get('/word-pairs/', (req, res)=>{
res.json(pairs(sample(friendlyWords.predicates), sample(friendlyWords.objects)));
});
app.get('/word-triples/', (req, res)=>{
res.json(triples(sample(friendlyWords.predicates),
sample(friendlyWords.predicates), sample(friendlyWords.objects)));
});
app.get('/objects/', (req,res)=>{
res.json(sample(friendlyWords.objects));
});
app.get('/predicates/', (req,res)=>{
res.json(sample(friendlyWords.predicates));
});
app.get('/team-pairs/', (req, res)=>{
res.json(pairs(sample(friendlyWords.predicates), sample(friendlyWords.teams)));
});
app.get('/teams/', (req, res)=>{
res.json(sample(friendlyWords.teams));
});
app.get('/collection-pairs/', (req, res)=>{
res.json(pairs(sample(friendlyWords.predicates), sample(friendlyWords.collections)));
});
app.get('/collections/', (req, res)=>{
res.json(sample(friendlyWords.collections));
});
// listen for requests :)
var listener = app.listen(process.env.PORT, function() {
console.log('Your app is listening on port ' + listener.address().port);
});