npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hapi-to

v1.0.0

Published

Creates URLs to named routes

Downloads

32

Readme

hapi-to Build Status

This plugin gives you an API for generating URLs dynamically in hapi using route ids and optional parameters, so you don't need to hardcode them into your views. This helps a bunch if you change the path structure of your routes, or move them into prefixed plugins in the future.

##Features

  • Let's you change paths without breaking existing links + redirects
  • 100% test coverage
  • Works with params (including wildcard, optional and multi-params)
  • Configurable with options

##Install

To download and add to dependencies run npm install --save hapi-to

Register with a server like any other hapi plugin with server.register():

var Hapi = require('hapi');

var server= new Hapi.Server();
server.connection({ port: 8080 });

server.register(require('hapi-to'), function (err) {
    
    if (err) {
        throw err;
    }
    
    server.start();
});

After registering, hapi-to will decorate the hapi request object with a new method: request.to().

##API

request.to(id, params, options)

Returns a URL to a route

  • id - required route id. Set when creating a route with the config.id property.
  • params - request parameters where:
    • query - an optional object of query string key-values. Will be stringified using the Qs module.
    • params - an optional object of path parameters. Number given must match route path. Each parameter value can be a string or an array of strings (for multi params)
  • options - additional options which affect how the URL is generated where:
    • rel - Whether to generate a relative URL. Can be true or false (default).
    • secure - a boolean or "match". If true the URL will be https, if false will be http. If "match" (the default) will match the x-forwarded-proto header or the current request's connection protocol (in that order).
    • host - string that sets the host in the URL. Default is to match the current request.

##Example

var Hapi = require('hapi');

var server = new Hapi.Server();
server.connection({ port: 8080 });

server.route([{
    method: 'GET',
    path: '/',
    handler: function (request, reply) {

        var url = request.to('hello', {
            params: { thing: 'stitch', num: 'nine' },
            query: { page: '1' }
        });

        reply.redirect(url);
    }
}, {
    method: 'GET',
    path: '/multi',
    handler: function (request, reply) {

        var url = request.to('rick', {
            params: { multi: ['never', 'gonna', 'give', 'you', 'up'] }
        });

        reply.redirect(url);
    }
}, {
    method: 'GET',
    path: '/a/{thing}/in/{num}/saves/time',
    config: {
        id: 'hello',
        handler: function (request, reply) {

            reply('You got here!');
        }
    }
}, {
    method: 'GET',
    path: '/{multi*5}',
    config: {
        id: 'rick',
        handler: function (request, reply) {

            reply('You got here!');
        }
    }
}]);

server.register(require('hapi-to'), function (err) {
    
    server.start();
});

If you run the above example and navigate to http://localhost:8080/ you will be redirected to http://localhost:8080/a/stitch/in/nine/saves/time?page=1

If you navigate to http://localhost:8080/multi you will be redirected to http://localhost:8080/never/gonna/give/you/up

Contributing

Issues and PRs are welcome. If you have any questions or suggestions please open an issue.