Skip to content

Commit f83dece

Browse files
Rolando Santamaria MasoRolando Santamaria Maso
authored andcommitted
adding target load balancer demo
1 parent 8be130c commit f83dece

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

demos/target-load-balancer.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict'
2+
3+
const gateway = require('../index')
4+
const { P2cBalancer } = require('load-balancers')
5+
6+
// @TODO: update the list of target origins
7+
const targets = [
8+
'http://localhost:3000',
9+
'https://httpbin.org'
10+
]
11+
const balancer = new P2cBalancer(targets.length)
12+
13+
gateway({
14+
routes: [{
15+
proxyHandler: (req, res, url, proxy, proxyOpts) => {
16+
proxyOpts.base = targets[balancer.pick()]
17+
18+
return proxy(req, res, url, proxyOpts)
19+
},
20+
prefix: '/balanced'
21+
}]
22+
}).start(8080).then(() => console.log('API Gateway listening on 8080 port!'))
23+
24+
const service = require('restana')({})
25+
service
26+
.get('/get', (req, res) => res.send({ msg: 'Hello from service 1!' }))
27+
.start(3000).then(() => console.log('Public service listening on 3000 port!'))
28+
29+
// Usage: curl 'http://localhost:8080/balanced/get'

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"fg-multiple-hooks": "^1.2.0",
5252
"helmet": "^4.2.0",
5353
"http-lambda-proxy": "^1.1.2",
54+
"load-balancers": "^1.3.52",
5455
"micromatch": "^4.0.2",
5556
"mocha": "^8.2.1",
5657
"nyc": "^15.1.0",

0 commit comments

Comments
 (0)