-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
72 lines (51 loc) · 1.59 KB
/
index.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
var fs = require("fs");
var express = require("express");
var cors = require('cors')
// the express stuff now
const app = express();
app.use(cors())
app.get('/words/fr_en/', (req, res) => {
// let wordsNum = req.params.amountOfWords;
// wordsNum ? wordsNum : 20;
fs.readFile("words.txt", "utf8", (err, data) => {
if (err) {
console.log(
"sorry body something has happend durign the opening of the words.txt file"
);
return;
}
let lines = data.split(/\n/g).map((line) => line.split(/\s/g));
const words = wordRandomizer(lines,20);
console.log(words);
res.status(200).json(words);
});
})
app.get('/phrases/en_fr/', (req, res) => {
// let phrasesNum = req.params.amountOfPhrases;
// phrasesNum ? phrasesNum : 20;
fs.readFile("phrases.txt", "utf8", (err, data) => {
if (err) {
res.write("sorry body something has happend durign the opening of the words.txt file")
return;
}
let lines = data.split(/\n/).map((line) => line.split("="));
const words = wordRandomizer(lines, 20);
res.status(200).json(words);
});
})
app.listen(8080, () => {
console.log('app running on 8080');
})
let wordRandomizer = (lines, numberOfWords) => {
let set = new Set();
// get a set of random but unique numbers
while (set.size !== numberOfWords) {
let rndmNum = Math.floor(Math.random() * lines.length);
set.add(rndmNum);
}
let finalArr = []
set.forEach((i) => {
finalArr.push(lines[i]);
});
return finalArr;
};