Skip to content

Commit b122abd

Browse files
Rolando Santamaria MasoRolando Santamaria Maso
authored andcommitted
adding hostnames support documentation
1 parent 838e0e3 commit b122abd

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,56 @@ gateway({
240240
```
241241
> In this example we have used the [express-rate-limit](https://www.npmjs.com/package/express-rate-limit) module.
242242
243+
## Hostnames support
244+
We can also implement hostnames support with fast-gateway, basically we translate hostnames to prefixes:
245+
```js
246+
...
247+
248+
// binding hostnames to prefixes
249+
const hostnames2prefix = [{
250+
prefix: '/api',
251+
hostname: 'api.company.tld'
252+
}]
253+
// instantiate hostnames hook, this will prefix request urls according to data in hostnames2prefix
254+
const hostnamesHook = require('fast-gateway/lib/hostnames-hook')(hostnames2prefix)
255+
256+
// separately instantiate and configure restana application
257+
const app = restana()
258+
const server = http.createServer((req, res) => {
259+
hostnamesHook(req, res, () => {
260+
return app(req, res)
261+
})
262+
})
263+
264+
// gateway configuration
265+
gateway({
266+
server: app, // injecting existing restana application
267+
routes: [{
268+
prefix: '/api',
269+
target: 'http://localhost:3000'
270+
}]
271+
})
272+
273+
...
274+
```
275+
> Afterwards:
276+
> `curl --header "Host: api.company.tld:8080" http://127.0.0.1:8080/api-service-endpoint`
277+
278+
You can optionally `npm install micromatch` and benefit from patterns support:
279+
```js
280+
const hostnames2prefix = [{
281+
prefix: '/admin',
282+
hostname: '*.admin.company.tld'
283+
}, {
284+
prefix: '/services',
285+
hostname: [
286+
'services.company.tld',
287+
'*.services.company.tld'
288+
]
289+
}]
290+
```
291+
For more details, please checkout the `basic-hostnames.js` demo.
292+
243293
## Gateway level caching
244294
Caching support is provided by the `http-cache-middleware` module. https://www.npmjs.com/package/http-cache-middleware
245295

demos/basic-hostnames.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ const restana = require('restana')
77

88
// binding hostnames to prefixes
99
const hostnames2prefix = [{
10-
prefix: '/public',
11-
hostname: 'nodejs.org'
10+
prefix: '/api',
11+
hostname: 'api.company.tld'
1212
}]
1313
// instantiate hostnames hook, this will prefix request urls according to data in hostnames2prefix
1414
const hostnamesHook = require('./../lib/hostnames-hook')(hostnames2prefix)
@@ -28,7 +28,7 @@ gateway({
2828
],
2929

3030
routes: [{
31-
prefix: '/public',
31+
prefix: '/api',
3232
target: 'http://localhost:3000'
3333
}]
3434
})

0 commit comments

Comments
 (0)