-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.js
More file actions
47 lines (41 loc) · 1.22 KB
/
server.js
File metadata and controls
47 lines (41 loc) · 1.22 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
const express = require('express');
const app = express();
const fs = require('fs');
const fsp = require("fs").promises;
const cors = require('cors');
const bodyParser = require('body-parser');
const jsonParser = bodyParser.json();
const urlencodedParser = bodyParser.urlencoded({ extended: false });
app.use(cors());
app.get('/', function(req, res) {
res.send('<h1>QUIZZ</h1> GET /api/quizz/:id POST /api/save');
});
app.post('/api/save', jsonParser,function(req, res) {
let id = Date.now();
fsp.writeFile('tmp/' + id + '.json',JSON.stringify(req.body))
.then(value => {
res.json({
error : 0,
id : id
});
})
.catch(reason => {
res.json({
error : 500,
id : id
});
});
});
app.get('/api/quizz/:id', function (req, res) {
fs.readFile('tmp/' + req.params.id + '.json', (err, data) => {
if (err) {
res.json({error : 404, message : err});
}else {
console.log('data',data)
res.json({error : 0, quizz : JSON.parse(data)});
}
});
});
app.listen(3006, function () {
console.log('Example app listening on port 3006!')
});