-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
106 lines (89 loc) · 3.16 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const express = require("express");
const app = express();
const fs = require("fs");
const PORT = process.env.PORT || 3000;
app.use(express.json())
// serve files in the public folder using express
app.use(express.static('public'));
app.get('/', (req, res) => {
res.sendFile('home.html', { root: __dirname + "/public"});
});
// fetch word function is received here, returns a random word
app.post("/word", (req, res) => {
let wordLength = req.body.word;
console.log("received word length: " + wordLength)
fs.readFile(__dirname + '/wordList.JSON', (error, data) => {
if (error) {
console.error(error)
return
}
try {
let words = JSON.parse(data)
const randomIndex = Math.floor(Math.random() * words[wordLength].length);
let randomWord = words[wordLength][randomIndex];
console.log("random word chosen: ", randomWord)
const responseString = randomWord.toLowerCase() ;
res.json({ responseString });
}
catch (parseError){
console.error(parseError)
}
})
})
// check if the word user input is in the list
app.post("/checkWord", (req, res) => {
let userWord = req.body.userWord.toLowerCase();
console.log("received userWord: " + userWord)
fs.readFile(__dirname + '/wordList.JSON', (error, data) => {
if (error) {
console.error(error)
res.status(500).send("Error reading word list");
return;
}
try {
let words = JSON.parse(data)
let validWords = new Map();
words['5'].forEach(word => validWords.set(word.toLowerCase(), true));
let response = validWords.has(userWord);
console.log(response ? "Valid word!" : "Invalid word!");
res.json( { response });
}
catch (parseError){
console.error(parseError);
res.status(500).send("Error parsing word list");
}
})
})
// when feedback page is loaded, returns the feedbacks store in the JSON file
app.get('/getData', function(req, res) {
fs.readFile(__dirname + '/feedbacks.JSON', function(error, data) {
if (error) {
throw error;
}
const jsonData = JSON.parse(data)
res.json(jsonData);
})
})
// when feedback is being submitted, the name and description along with time is added to JSON file
app.post('/addFeedback', function(req, res){
// Get form data from request body
const name = req.body.name;
const feedback = req.body.feedback;
const time = req.body.time;
// Load existing feedback data from JSON
const feedbackData = fs.readFileSync(__dirname + '/feedbacks.JSON');
const feedbacks = JSON.parse(feedbackData);
// Add new feedback to array
feedbacks.push({
name: name,
feedback: feedback,
time: time
})
// Save updated feedback data to JSON file
fs.writeFileSync( __dirname + '/feedbacks.JSON', JSON.stringify(feedbacks, null, 2));
// Send response indicating success
res.sendStatus(200);
})
app.listen(PORT, () => {
console.log(`Microservice is listening on port ${PORT}`);
});