-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
57 lines (49 loc) · 1.22 KB
/
app.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
"use strict";
var express = require('express');
var cors = require('cors');
var app = express();
var wordArr = [];
function getRandomWords(amount){
//Shuffle the array
wordArr.sort( () => (0.5 - Math.random()) );
return wordArr.slice(0, amount);
}
app.use(cors());
app.get('/', function(req, res){
res.send("To show this is working");
})
app.get('/word', function (req, res) {
if (req.query.all){
console.log("Get all words");
res.json(wordArr);
}
else {
let amount = req.query.amount;
console.log(`Requested ${amount} words`);
if (wordArr.length<amount){
console.error('Not enough words');
}
else {
res.json(getRandomWords(amount));
}
}
});
app.post('/word', function(req, res){
console.log('pushed!');
wordArr.push(req.query.word);
res.json({message: `Added word: ${req.query.word}`});
});
app.delete('/word', function(req, res){
let word = req.query.word;
var index = wordArr.indexOf(word);
if (index===-1){
res.json({message: `No such word as ${word}`});
}
else {
wordArr.splice(index, 1);
res.json({message: `Successfully deleted ${word}`});
}
})
app.listen(process.env.PORT, function () {
console.log('Example app listening on port 3000!');
});