reverse-proxy-listener
v0.0.1
Published
Simple load balancing reverse proxy RequestListener based on pure Node.js.
Downloads
4
Readme
Reverse Proxy Listener
Simple load balancing reverse proxy RequestListener based on pure Node.js.
WARNNING: Do not use this development listener in a production environment!
API
- reverse-proxy-listener([UpstreamServers]) => http.RequestListener
interface UpstreamServer {
server: string; // the address of remote server, as the backend.
weight?: number; // the weight of the server, by default, 1.
}
Usage
// config.js
module.exports = [
{ server: '127.0.0.1:8081', weight: 5 },
{ server: '127.0.0.1:8082', weight: 1 },
{ server: '127.0.0.1:8083', weight: 1 }
]
As a server.
const http = require('http');
const createRPL = require('reverse-proxy-listner');
const config = require('./config');
const proxyListener = createRPL(config);
const server = http
.createServer(proxyListener)
.listen(8080);
As a middleware.
const Koa = require('koa');
const Router = require('koa-router');
const createRPL = require('reverse-proxy-listner');
const config = require('./config');
const proxyListener = createRPL(config);
const app = new Koa();
const router = new Router();
router.use('/proxy', (ctx, next) => {
return proxyListener(ctx.req, ctx.res);
})
app.use(router.routes());