forked from gen-failure/growduino-client
-
Notifications
You must be signed in to change notification settings - Fork 5
/
proxy.js
60 lines (48 loc) · 1.88 KB
/
proxy.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
var http = require('http'),
httpProxy = require('http-proxy');
var connect = require('connect');
var bodyParser = connect.bodyParser();
var request = require('request');
//var TARGET = 'arduino.natur.cuni.cz';
//var TARGET = '78.108.106.180';
//var TARGET = '192.168.1.44';
var TARGET = '185.47.222.180:2346';
var proxy = httpProxy.createProxyServer({target: "http://"+TARGET});
var express = require('express');
var app = express();
/* special method needs because of strange arguino connection closing */
function proxyPost(req, res) {
bodyParser(req, res, function() {
request({
method: 'POST',
url: 'http://'+TARGET+req.url,
json: req.body,
headers: {
'Accept': 'application/json, text/plain, */*',
'Origin': 'http://'+TARGET,
'Referer': 'http://'+TARGET+'/'
}
}, function (error, response, body) {
res.statusCode = 200;
res.end();
});
});
}
app.get(/^\/(sensors|triggers|alerts|DATA|config.jso|client.jso|wifilist.jso|wifi_active.jso|partial\/config.jso|send_test_mail)/, function(req, res) {
proxy.proxyRequest(req, res);
});
app.post(/^\/(sensors|triggers|alerts|DATA|config.jso|client.jso|wifilist.jso|wifi_active.jso|partial\/config.jso|send_test_mail)/, function(req, res) {
proxyPost(req, res);
});
app.use('/bower', express.static(__dirname + '/bower83'));
app.use('/bower_components', express.static(__dirname + '/bower_components'));
app.use('/fonts', express.static(__dirname + '/bower_components/bootstrap/fonts'));
app.get('/js/settings.js', function(req, res) {
res.sendfile('./src/js/settings.js');
});
app.use(express.static(__dirname + '/src'));
//app.use(express.static(__dirname + '/'));
//app.use(express.static(__dirname + '/dist'));
app.listen(8000, function () {
console.log("listening on port 8000");
});