-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
43 lines (33 loc) · 1.23 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
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const app = express();
const port = process.env.PORT || 3000
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));
// Serve index.html for the root route
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
// Import the sample data
const sampleData = require(path.join(__dirname, 'sampledata.json'));
// Add the /questions endpoint
app.get('/questions', (req, res) => {
// Send the questions without the correctAnswer field
const questionsWithoutAnswers = sampleData.map(({ id, question, simplifiedChoices, explanation }) => ({ id, question, simplifiedChoices, explanation }));
res.json(questionsWithoutAnswers);
});
// Add the /checkAnswer endpoint
app.post('/checkAnswer', (req, res) => {
const { questionId, answer } = req.body;
const question = sampleData.find(q => q.id === questionId);
if (!question) {
res.status(404).json({ error: 'Question not found' });
return;
}
const correct = question.correctAnswer === answer;
res.json({ correct });
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});