This repository has been archived by the owner on Aug 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
78 lines (66 loc) · 2.04 KB
/
app.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const fs = require('fs');
const http = require('http');
const express = require('express');
const helmet = require('helmet');
const bodyParser = require('body-parser');
const multipart = require('connect-multiparty');
const config = require('./lib/config');
const util = require('./lib/util');
const rates = require('./lib/rate');
const multipartMiddleware = multipart();
const app = express();
app.use(express.static('public'));
app.use('/components', express.static('node_modules'));
app.use(helmet());
app.disable('x-powered-by');
app.get('/rateLists', (req, res) => {
const result = {};
result.count = rates.length;
result.rates = rates;
res.json(result);
});
app.post('/converter', multipartMiddleware, (req, res) => {
const xlsPath = req.files.xls.path;
let bills = util.parseXLS(xlsPath);
fs.unlinkSync(xlsPath);
bills = util.processBills(bills);
res.json(bills);
});
app.post('/calculate', bodyParser.json(), (req, res) => {
const { bills, start, end } = req.body;
res.json(util.calculateTotalRebate(bills, start, end));
});
app.post('/feedback', multipartMiddleware, (req, res) => {
let currentFeedback;
if (Object.keys(req.body).length === 0) {
res.status(500).send('Empty request.');
return;
}
fs.readFile('./feedback.json', (readErr, data) => {
if (readErr) {
res.status(500).send('Error occurred while open file.');
return;
}
currentFeedback = JSON.parse(data);
currentFeedback.push({
name: req.body['input-name'],
item: req.body['input-store'],
detail: req.body['input-detail'],
rate: req.body['input-rate'],
comment: req.body['input-comment']
});
currentFeedback = JSON.stringify(currentFeedback, null, 2);
fs.writeFile('./feedback.json', currentFeedback, (writeErr) => {
if (writeErr) {
res.status(500).send('Error occurred while write file.');
return;
}
res.json({
success: true
});
});
});
});
http.createServer(app).listen(config.port, () => {
console.log('App listening on port', config.port);
});