-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
74 lines (66 loc) · 1.97 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
var express = require('express');
//var logfmt = require('logfmt');
//var bodyParser = require('body-parser');
var cors = require('cors');;
var request = require('request');
var app = express();
const port = process.env.HNOSS_PORT || process.env.PORT || 8888;
const dir = process.env.HNOSS_DIR || 'dist';
let api = process.env.HNOSS_API || "https://api.eshacloud.com:30261";
var apiUsername = process.env.HNOSS_API_USERNAME;
var apiPassword = process.env.HNOSS_API_PASSWORD;
var apiOptions = {};
if (apiUsername && apiPassword) {
apiOptions.auth = {
user: apiUsername,
pass: apiPassword
};
}
function toURL(req) {
return req.originalUrl.replace('/api/', api);
}
function log(req) {
const url = toURL(req);
if (req.method === 'OPTIONS') {
return { method: req.method, url };
}
return {
method: req.method,
url,
headers: req.headers,
body: typeof req.body === "string" ? req.body : JSON.stringify(req.body)
};
}
function print(log) {
console.log(log.method, log.url);
if (log.headers) {
for (const key in log.headers) {
if (!log.headerFilter || log.headerFilter.contains(key)) {
console.log(key, ': ', log.headers[key]);
}
}
}
if (log.body) {
console.log(typeof log.body === "string" ? log.body : JSON.stringify(log.body));
}
console.log('\n');
}
app.use(cors());
//app.use(bodyParser.text({ type: ['text/*', 'application/soap+xml'] }));
//app.use(bodyParser.json());
//app.use(logfmt.requestLogger({immediate: true}, log));
app.all('/api/*', function (req, res) {
const url = toURL(req);
const method = req.method.toLowerCase();
print(log(req))
try {
req.pipe(request[method](url, apiOptions))
.pipe(res);
} catch (e) {
res.status(500).send(e.toString()).end();
}
})
app.use(express.static(dir));
app.listen(port, function () {
console.log('Using API:', api, 'on port', port);
});