@@ -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
244294Caching support is provided by the ` http-cache-middleware ` module. https://www.npmjs.com/package/http-cache-middleware
245295
0 commit comments