forked from evolify/react-mobx-ts-antd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
76 lines (63 loc) · 2.31 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
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
let express = require('express'),
app = express(),
path = require('path'),
port = process.env.PORT ? process.env.PORT : 9001,
dist = path.join(__dirname, 'dist'),
bodyParser = require('body-parser'),
proxy = require('http-proxy-middleware'),
hmacSHA256 = require('crypto-js/hmac-sha256'),
CryptoJS = require('crypto-js'),
moment = require('moment');
app.use(express.static(dist));
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
let url = "http://127.0.0.1:1323",
webContext = "",
ACCESS_KEY = 'app1',
ACCESS_SECRET = 'Lx1b8JoZoE',
timestamp = moment().format('YYYY-MM-DD HH:mm:ss');
let options = {
target: url,
changeOrigin: true,
ws: true,
pathRewrite: {
'^/api': '' // Host path & target path conversion
},
proxyTimeout: 1000,
onProxyReq: function (proxyReq, req, res) {
console.log("Request incoming, redirecting to: " + url + req.url);
let resource = webContext + req.url;
let body = req.body;
let signBody;
let contentType = req.headers['content-type'];
if (contentType == 'text/plain') {
signBody = body;
} else {
signBody = JSON.stringify(body);
if (signBody === '{}') {
signBody = ""
}
}
console.log("signature: " + req.method + "\n" + signBody + "\n" + resource + "\n" + timestamp + "\n");
let signature = hmacSHA256(req.method + "\n" + signBody + "\n" + resource + "\n" + timestamp + "\n", ACCESS_SECRET);
// Write out body changes to the proxyReq stream
proxyReq.setHeader("appid", ACCESS_KEY);
proxyReq.setHeader("timestamp", timestamp);
proxyReq.setHeader("signature", CryptoJS.enc.Base64.stringify(signature));
proxyReq.setHeader('Content-Length', Buffer.byteLength(signBody));
proxyReq.write(signBody);
proxyReq.end();
}
};
let proxyServer = proxy(options);
app.all('/api/*', proxyServer);
// app.get('/', function (req, res) {
// res.sendFile(path.join(dist, 'index.html'));
//
// });
app.listen(port, function (error) {
if (error) {
console.log(error); // eslint-disable-line no-console
}
console.info('Express is listening on port %s.', port); // eslint-disable-line no-console
});