-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
79 lines (60 loc) · 2.18 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
79
import express from 'express';
import bodyParser from 'body-parser';
import logger from 'morgan';
import favicon from 'serve-favicon';
import helmet from 'helmet';
import compression from 'compression';
import path from 'path';
import env from './config/env';
const fs = require('fs');
const app = express();
/*==================================
= Middleware =
==================================*/
app.use(favicon(path.join(__dirname, 'favicon.ico')));
app.use(helmet()); // Helmet helps you secure your Express apps by setting various HTTP headers.
app.use(compression());
app.use(logger('tiny'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// serve static files, this is for frontend React
app.use('/static', express.static(path.join(__dirname, 'public', 'static')));
/*===== End of Middleware ======*/
/*===========================================
= Basic Authentication =
===========================================*/
//app.use(require('node-basicauth')({'haochuan': 'password'}));
/*===== End of Basic Authentication ======*/
/*===========================
= COR =
===========================*/
// app.use(require('cors')());
/*===== End of COR ======*/
/*===========================
= ROUTES =
===========================*/
app.post('/post_ethervote', function(req, res) {
//console.log(req.body);
fs.writeFile(__dirname+"/smartContractData.json", JSON.stringify(req.body), "utf8", function(error){
if (error) res.status(500).json(error);
else res.status(200).send("Data written in file");
});
});
app.get('/get_ethervote', function (req, res) {
fs.readFile(__dirname+"/smartContractData.json", "utf8", function (error, response) {
if (!error) {
res.status(200).json(JSON.parse(response));
} else {
res.status(500).send("error");
}
});
});
/*===== End of Routes ======*/
// Load React App
// Serve HTML file for production
if (env.name === 'production') {
app.get('*', function response(req, res) {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
}
export default app;