Skip to content

Instantly share code, notes, and snippets.

@tckz
Last active December 13, 2015 23:49
Show Gist options
  • Save tckz/4994696 to your computer and use it in GitHub Desktop.
Save tckz/4994696 to your computer and use it in GitHub Desktop.
node.jsでhttpリクエストとwebsocket両方をreverse proxy
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