-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
41 lines (33 loc) · 1.11 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
const express = require("express");
const app = express();
const questions = require('./data/questions.json');
const _map = require('lodash/map');
const _omit = require('lodash/omit');
const _differenceBy = require('lodash/differenceBy');
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.set("port", process.env.PORT || 3001);
// Express only serves static assets in production
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
}
app.get("/api/questions", (req, res) => {
const data = _map(questions, (question) => _omit(question, 'answer'));
res.json({
data
});
});
app.post("/api/score", (req, res) => {
const data = req.body;
const incorrectAnswers = _differenceBy(questions, data, 'answer');
const incorrect = incorrectAnswers.length;
const correct = (questions.length - incorrect);
res.json({
correct,
incorrect,
score: correct * 5
});
})
app.listen(app.get("port"), () => {
console.log(`Find the server at: http://localhost:${app.get("port")}/`); // eslint-disable-line no-console
});