Magic Reverse Proxy
Getting started into Magic Reverse Proxy!
Magic Reverse Proxy is a proxy that can be used to forward requests to another server by domain name, and have a load balancer, ssl/tls support features.
Installation
You must run the following terminal command in same path of your project.
npm install magic-reverse-proxy --save
How it works?
In this basic sample our main goal is to forward all websocket traffic from port 8080 to port 1234.
Creating a basic WebSocket server:
import http from 'http';
import WebSockets from 'websocket';
import { createProxy } from 'magic-reverse-proxy';
const server = http.createServer((req, res) => {
res.writeHead(403).end();
});
server.listen(1234, function () {
console.log('Destination server is running on port 1234!');
});
const WebSockServer = new WebSockets.server({
httpServer: server,
maxReceivedFrameSize: 65536,
maxReceivedMessageSize: 65536
});
WebSockServer.on('request', (request) => {
console.log('WebSocket connected over [Client -> localhost:8080 -> localhost:1234]');
let sock = request.accept();
sock.on('message', (msg) => {
console.log(msg.utf8Data);
})
});
Creating the Magic Proxy trigger and binding to specific port:
let proxy = createProxy({
allow_unknown_host: false,
allow_websockets: true,
http: {
port: 8080,
enabled: true,
start_callback: () => {
console.log('Magic proxy server is running on port 8080!');
},
middlewares: []
},
proxies: [
{
domain: '*',
timeout: 10000,
round: 0,
destination: ['http://localhost:1234/'],
sockDestination: ['ws://localhost:1234']
}
],
default_proxy: {
timeout: 10000,
round: 0,
destination: ['http://localhost:1234/']
}
});
proxy.bind();