Last active
February 6, 2024 18:56
-
-
Save kevinswiber/15e6b6d07c484d00101041eb4ebcf434 to your computer and use it in GitHub Desktop.
Express Gateway Example with Multiple Services
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const forum = express(); | |
forum | |
.get('/healthz', (req, res, next) => { | |
res.send({ name: 'forum', status: 'healthy' }); | |
next(); | |
}) | |
.get('/d/:id', (req, res, next) => { | |
res.send({ discussion: req.params.id }); | |
next(); | |
}) | |
.listen(process.env.PORT || 1337); | |
const members = express(); | |
members | |
.get('/', (req, res, next) => { | |
res.send({ members: [ { name: 'gary', avatar: 'snail' }] }); | |
next(); | |
}) | |
.listen(process.env.PORT2 || 1338); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http: | |
port: 8080 | |
admin: | |
port: 9876 | |
hostname: localhost | |
apiEndpoints: | |
forumAPI: | |
host: 'forum.example.com' | |
paths: | |
- '/discussions/*' | |
- '/healthz' | |
membersAPI: | |
host: 'members.example.com' | |
serviceEndpoints: | |
forumService: | |
url: 'http://localhost:1337' | |
membersService: | |
url: 'http://localhost:1338' | |
policies: | |
- expression | |
- key-auth | |
- proxy | |
pipelines: | |
- name: forum | |
apiEndpoints: | |
- forumAPI | |
policies: | |
- expression: | |
- action: | |
jscode: | | |
if (req.url.startsWith('/discussions')) { | |
const slug = req.url.substr('/discussions'.length); | |
req.url = '/d' + slug; | |
} | |
- proxy: | |
- action: | |
serviceEndpoint: forumService | |
- name: members | |
apiEndpoints: | |
- membersAPI | |
policies: | |
- key-auth: | |
- proxy: | |
- action: | |
serviceEndpoint: membersService |
Hi, thanks for the gist, can we set up to send a request without a host header? (Also many pipelines)
If you have in members service the same "/healthz" route, how did you route it then in express-gateay?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Testing
forumAPI
healthcheck:Testing
forumAPI
discussions:Testing
membersAPI
root:Testing
membersAPI
root with authorization: