-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
74 lines (68 loc) · 2.08 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
//Install express server
const express = require('express');
const http = require('http');
const bodyParser = require('body-parser');
const cors = require('cors');
const path = require('path');
const https = require('https');
const compression = require('compression');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.text({ type: 'text/*' }))
app.use(compression());
const fs = require('fs');
const { createProxyMiddleware } = require('http-proxy-middleware');
const appPath = '/dist/ng-visuall';
app.use(express.static(__dirname + appPath));
app.get('/urlquery/*', function (req, res) {
let reqURL = req.url.substr(10);
const data = [];
if (reqURL.substr(0, 5).toLowerCase() != "https" && reqURL.substr(0, 4).toLowerCase() == "http") {
let request = http.request(reqURL, function (response, body) {
response.on('data', function (chunk) {
data.push(chunk);
});
response.on('end', function () {
let result = JSON.parse(data.join(''))
res.send(result);
});
});
request.on('error', function (e) {
console.log(e.message);
});
request.end();
}
else {
if (reqURL.substr(0, 4).toLowerCase() != "http") {
reqURL = "https://" + reqURL;
}
let request = https.request(reqURL, function (response, body) {
response.on('data', function (chunk) {
data.push(chunk);
});
response.on('end', function () {
try {
const result = JSON.parse(data.join(''))
res.send(result);
} catch (e) {
res.send('{}');
}
});
});
request.on('error', function (e) {
console.log(e.message);
});
request.end();
}
});
app.get('/e2e', function (req, res) {
res.sendFile(path.join(__dirname + '/e2e-results.txt'));
});
app.get('/*', function (req, res) {
res.sendFile(path.join(__dirname + appPath + '/index.html'));
});
const port = process.env.PORT || 4400;
app.listen(port);
// Start the app by listening on the default port
console.log('server listening port: ', port);