Skip to content

Instantly share code, notes, and snippets.

@icantrank
Created January 25, 2021 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icantrank/d365cd5ee815c104ed2382ffd062993a to your computer and use it in GitHub Desktop.
Save icantrank/d365cd5ee815c104ed2382ffd062993a to your computer and use it in GitHub Desktop.
websocket proxy nodejs
// proxies requests to /folder to / on 8081
// proxies requests to /folder2 to /folder2/ on 8081
// all other requests sent to 8080
// listens on 80
var http = require('http');
//2 dependencies, install them
var proxyRules = require('http-proxy-rules')
var websocket = require('http-proxy');
var rules = new proxyRules({
//rules work like nginx or apache rules, just regex
rules: {
'/folder(.*)': 'http://localhost:8081$1', //app sees '/ + req'
'(/folder2.*)': 'http://localhost:8082$1' //app sees '/folder2/ + req'
},
default: 'http://localhost:8080' // default target like main site
})
var server = websocket.createProxy();
server.on('error', function(err,req,res) {
console.log(' - ', err);
});
http.createServer(function(req, res) {
// match method comes from proxy rules to test rules
var target = rules.match(req);
if (target) {
console.log('GET', req.url)
return server.web(req, res, {
target: target
});
}
// if that didn't work we fkd up
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('Internal Server Error');
}).listen(80);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment