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-route-hierarchy

v2.0.1

Published

Hapi plugin that automatically sets up your routes based on your directory hierarchy.

Downloads

3

Readme

hapi-route-hierarchy

Hapi plugin that automatically sets up your routes based on your directory hierarchy.

What is this?

The purpose of this plugin is to allow intuitive route setup by basing routes on your directory structure. For example, imagine you have a Hapi project with this folder structure:

my_hapi_project
│   server.js
│
└───routes
    │   login.js
    │
    └───users
        │   group_a.js
        │   group_b.js

Assume that server.js simply starts the Hapi server, after registering the plugin like this:

server.register(
    {
        plugin: require( 'hapi-route-hierarchy' ),
        options: {
            root: __dirname + '/routes',
            glob_pattern: '**/*.js'
        }
    }, ( err ) => {
        // Start the server...
    }
)

All the files under /routes include the data for all the routes. So for example, /routes/login.js might look like this:

module.exports = {
    method: 'GET',
    path: '/login',
    handler: function( request, h ) {
        return h.response( 'You have been authenticated!' );
    }
};

This will result in a route at /login, which is rather boring, but what if we put something similar in /routes/users/group_a.js:

module.exports = {
    method: 'GET',
    path: '/group_a',
    handler: function( request, h ) {
        return h.response( 'Here is a list of all users in group A!' );
    }
};

This would result in a route at /users/group_a.

In this way you can easily keep your routes organized by ensuring they always match your directory structure.

Plugin Options

root

  • REQUIRED

  • Type: string

  • Description: The directory to search for files with route data. This can accept 2 types of paths:

    • Relative path from process.cwd()
    • Absolute path

glob_pattern

  • REQUIRED
  • Type: string
  • Description: The glob pattern to use to determine which files should be loaded as route files.

glob_options

Route files

The route files can define route data in 3 different ways:

Object

module.exports = {
    method: 'GET',
    path: '/some_path',
    handler: function( request, h ) {
        // Do something...
    }
};

Array

module.exports = [
    {
        method: 'GET',
        path: '/some_path',
        handler: function( request, h ) {
            // Do something...
        }
    },
    {
        method: 'POST',
        path: '/some_other_path',
        handler: function( request, h ) {
            // Do something...
        }
    }
];

Function

Note that this variation allows you to access the Hapi server object

module.exports = function( server ){
    return {
        method: 'GET',
        path: '/some_path',
        handler: function( request, h ) {
            let some_fancy_plugin = request.server.plugins['some_fancy_plugin'];
            h.response( some_fancy_plugin.doFancyThings() );
        }
    };
};

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality.