-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
66 lines (50 loc) · 1.78 KB
/
Copy pathserver.js
File metadata and controls
66 lines (50 loc) · 1.78 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
const express = require('express');
const app = express();
const friendlyWords = require('friendly-words');
const sampleSize = require('lodash.samplesize');
// 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();
});
// Host our static site, thereby providing our html, css, and js.
app.use(express.static('public'));
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;
}
app.get('/word-pairs/', (req, res)=>{
res.json(pairs(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);
});