forked from alura-cursos/nodejs-serverless-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.mjs
More file actions
50 lines (44 loc) · 1.2 KB
/
Copy pathindex.mjs
File metadata and controls
50 lines (44 loc) · 1.2 KB
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
import { randomUUID } from 'crypto'
import express from 'express'
import { join, dirname } from 'path'
import { fileURLToPath } from 'url'
const app = express()
const __dirname = dirname(fileURLToPath(import.meta.url))
const correctQuestions = [3, 1, 0, 2]
const previousResults = new Map()
app.use(express.json())
app.post('/api/results', (req, res) => {
const { name, answers } = req.body
const correctAnswers = answers.reduce((acc, answer, index) => {
if (answer === correctQuestions[index]) {
acc++
}
return acc
}, 0)
const result = {
name,
correctAnswers,
totalAnswers: answers.length
}
const resultId = randomUUID()
previousResults.set(resultId, { response: req.body, result })
console.log(previousResults)
res.status(201).json({
resultId,
__hypermedia: {
href: `/results.html`,
query: { id: resultId }
}
})
})
app.get('/api/results/:id', (req, res) => {
const result = previousResults.get(req.params.id)
if (!result) {
return res.status(404).json({ error: 'Result not found' })
}
res.json(result)
})
app.use(express.static(join(__dirname, 'interface')))
app.listen(process.env.PORT || 3000, () => {
console.log('Server started')
})