hapi-url

1.0.2 • Public • Published

Hapi.js URL resolver

URL resolver for Hapi.js

How to use

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

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

Or, you can also write as:

const HapiUrl = require('hapi-url');
console.log(HapiUrl.current(request));
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:

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

Example

const Hapi = require('hapi');
const HapiUrl = require('hapi-url');
 
const server = new Hapi.Server({port: 3000});
 
server.route({
    method: 'GET',
    path: '/{path*}', // match some path
    handler: function(request, h) {
        return h.response(HapiUrl.current(request));    // reply with current URL
    }
});
 
server.start();

Dependents (0)

Package Sidebar

Install

npm i hapi-url

Weekly Downloads

156

Version

1.0.2

License

MIT

Unpacked Size

7.84 kB

Total Files

4

Last publish

Collaborators

  • rigon