Last active
December 13, 2015 23:49
-
-
Save tckz/4994696 to your computer and use it in GitHub Desktop.
node.jsでhttpリクエストとwebsocket両方をreverse proxy
This file contains hidden or 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'); | |
// Proxy to rails app | |
var proxy1 = new httpProxy.HttpProxy({ | |
target: { | |
host: 'localhost', | |
port: 3000 | |
} | |
}); | |
// Proxy to node.js | |
var proxy2 = new httpProxy.HttpProxy({ | |
target: { | |
host: 'localhost', | |
port: 8080 | |
} | |
}); | |
var regex_socket_io = /^\/socket\.io\//; | |
var server = http.createServer(function (req, res) { | |
if (req.url.match(regex_socket_io)) { | |
proxy2.proxyRequest(req, res); | |
} else { | |
proxy1.proxyRequest(req, res); | |
} | |
}); | |
server.on('upgrade', function(req, socket, head) { | |
proxy2.proxyWebSocketRequest(req, socket, head); | |
}); | |
server.listen(80); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment