Last active
August 29, 2015 14:25
-
-
Save kartben/32d972ba164600ce9462 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'), | |
httpProxy = require('http-proxy'), | |
connect = require('connect'); | |
//var morgan = require('morgan') | |
var proxy = httpProxy.createProxyServer({ | |
target: 'http://localhost:8080' // openHAB/SmartHome URL | |
}); | |
var app = connect(); | |
//app.use(morgan('combined')); | |
var filterDeleteRequests = | |
function(req, res, next) { | |
if (req.method == 'DELETE') { | |
res.statusCode = 403; | |
res.end(); | |
} else { | |
next(); | |
} | |
}; | |
var filterPutPostRequests = | |
function(req, res, next) { | |
if (req.method == 'POST' || req.method == 'PUT') { | |
// only allow to modify existing things (/rest/items) and to perform discovery requests | |
if (req.url.indexOf('/rest/items') == -1 && req.url.indexOf('/rest/discovery') == -1) { | |
res.statusCode = 403; | |
res.end(); | |
} | |
} else { | |
next(); | |
} | |
}; | |
app.use(filterDeleteRequests); | |
app.use(filterPutPostRequests); | |
app.use( | |
function(req, res) { | |
proxy.web(req, res); | |
} | |
); | |
http.createServer(app).listen(8003); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment