Skip to content

Instantly share code, notes, and snippets.

@kevinswiber
Last active February 6, 2024 18:56
Show Gist options
  • Save kevinswiber/15e6b6d07c484d00101041eb4ebcf434 to your computer and use it in GitHub Desktop.
Save kevinswiber/15e6b6d07c484d00101041eb4ebcf434 to your computer and use it in GitHub Desktop.
Express Gateway Example with Multiple Services
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);
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
@kevinswiber
Copy link
Author

kevinswiber commented Aug 4, 2017

Testing forumAPI healthcheck:

$ curl -i -H "Host: forum.example.com" http://localhost:8080/healthz
HTTP/1.1 200 OK
x-powered-by: Express
content-type: application/json; charset=utf-8
content-length: 35
etag: W/"23-oI8lro08GsI4wRbkbqIMZn3a0nY"
date: Fri, 04 Aug 2017 22:31:08 GMT
connection: close

{"name":"forum","status":"healthy"}                                                   

Testing forumAPI discussions:

$ curl -i -H "Host: forum.example.com" http://localhost:8080/discussions/nodejs
HTTP/1.1 200 OK
x-powered-by: Express
content-type: application/json; charset=utf-8
content-length: 23
etag: W/"17-aGi2PapXcBJJzJGz6x1KK0JP+WQ"
date: Fri, 04 Aug 2017 23:14:42 GMT
connection: close

{"discussion":"nodejs"}

Testing membersAPI root:

$ curl -i -H "Host: members.example.com" http://localhost:8080/
HTTP/1.1 401 Unauthorized
X-Powered-By: Express
Content-Type: text/html; charset=utf-8
Content-Length: 9
ETag: W/"9-PatfYBLj4Um1qTm5zrukoLhNyPU"
Date: Fri, 04 Aug 2017 22:31:12 GMT
Connection: keep-alive

Forbidden

Testing membersAPI root with authorization:

$ curl -i -H "Host: members.example.com" -H "Authorization: APIKey $APIKEY" http://localhost:8080/
HTTP/1.1 200 OK
x-powered-by: Express
content-type: application/json; charset=utf-8
content-length: 46
etag: W/"2e-O6rO+xob/EVDxdaV4yvvnL61o1Y"
date: Fri, 04 Aug 2017 23:25:28 GMT
connection: close

{"members":[{"name":"gary","avatar":"snail"}]}

@dyrkow
Copy link

dyrkow commented Sep 6, 2018

Hi, thanks for the gist, can we set up to send a request without a host header? (Also many pipelines)

@ramiz4
Copy link

ramiz4 commented Nov 14, 2019

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