-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
63 lines (53 loc) · 1.41 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
const { Server } = require('socket.io');
const io = new Server({ /* options */ });
let gSocket = null;
io.on('connection', (socket) => {
console.log('Connect from '+socket.id);
gSocket = socket;
});
io.listen(8888);
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const http = require('http');
const TARGET_HOST = '192.168.1.46';
const TARGET_PORT = 8000;
app.set('port', 8000);
app.use(bodyParser.json());
app.use(function (req, res, next) {
if (gSocket == null) {
res.writeHead(500, {});
res.write(JSON.stringify({
msgCode: 10001,
msgResp: 'Server not found'
}));
res.end();
return;
}
// Ask client to make http request
var headers = req.headers;
delete headers.host;
delete headers['content-length'];
var req = {
secure: false,
host: TARGET_HOST,
port: TARGET_PORT,
path: req.path,
method: req.method,
headers: req.headers,
body: req.body,
};
gSocket.emit('http-request', JSON.stringify(req));
// Listen response from client
gSocket.on('http-response', (msg) => {
var resp = JSON.parse(msg);
res.writeHead(resp.status, resp.headers);
res.write(JSON.stringify(resp.body));
res.end();
gSocket.removeAllListeners();
});
});
const httpServer = http.createServer(app);
httpServer.listen(app.get('port'), function() {
console.log('Listening on port ' + app.get('port'));
});