项目作者: rigon

项目描述 :
URL resolver for Hapi.js
高级语言: JavaScript
项目地址: git://github.com/rigon/hapi-url.git
创建时间: 2018-03-22T23:14:24Z
项目社区:https://github.com/rigon/hapi-url

开源协议:

下载


Hapi.js URL resolver

URL resolver for Hapi.js

How to use

Returns an URL object representing the current server’s URL:

  1. const HapiUrl = require('hapi-url');
  2. console.log(HapiUrl(request));
  3. console.log(HapiUrl(request).format());
  4. console.log(HapiUrl(request).resolve("/path"));

Or, you can also write as:

  1. const HapiUrl = require('hapi-url');
  2. console.log(HapiUrl.current(request));
  3. console.log(HapiUrl.resolve(request, "/path"));

hapi-url looks for x-forwarded-proto and x-forwarded-host to resolve the current URL correctly when behind a reverse proxy.

If hapi-url is not clever enought for your case, you can override the protocol, host and basePath. basePath can be used to resolve the URL when the proxy adds a prefix in the path:

  1. const HapiUrl = require('hapi-url');
  2. Hapi.init({
  3. protocol: "https",
  4. host: "hiddenproxy.example.com",
  5. basePath: "/proxy/path/"
  6. })
  7. console.log(HapiUrl.format(request));

Example

  1. const Hapi = require('hapi');
  2. const HapiUrl = require('hapi-url');
  3. const server = new Hapi.Server({port: 3000});
  4. server.route({
  5. method: 'GET',
  6. path: '/{path*}', // match some path
  7. handler: function(request, h) {
  8. return h.response(HapiUrl.current(request)); // reply with current URL
  9. }
  10. });
  11. server.start();