-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
42 lines (35 loc) · 1.1 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
const express = require('express');
const fs = require('fs');
const Papa = require('papaparse');
const path = require('path');
const csvFilePath = 'data.csv';
const app = express();
app.use(express.json());
app.use(express.static(path.join(__dirname, 'dist')));
app.use(express.urlencoded({extended: false}));
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.get('/data', function (req, res) {
const csv = fs.readFileSync(csvFilePath, 'utf8');
const parsed = Papa.parse(csv);
res.send(parsed.data);
});
app.post('/data', function (req, res) {
const data = Papa.unparse(req.body);
try {
fs.writeFileSync(csvFilePath, data, 'utf8');
res.send();
} catch (error) {
res.status(403).end();
}
});
app.get('/*', function (req, res) {
res.sendFile(path.join(__dirname, 'dist', 'index.html'));
});
let port = Number(3003);
app.listen(port, function () {
console.log('Server running at localhost:' + port);
});