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

require-hapiroutes

v1.0.0

Published

Module to help organize and manage Hapi route definitions

Downloads

560

Readme

require-hapiroutes

Build Status

A module based on require-directory to load and manage hapi route definitions

Installation

npm install require-hapiroutes --save

Usage

In the directory you want to define your routes, create an index.js file with the following code in it. This will load all the routes in the directory (as well as all the modules like require-directory does).

//required file for require-hapiroutes.  Make it easier to setup routes to js files

var requireHapiRoutes = require('require-hapiroutes');
module.exports = requireHapiRoutes(module);

In your module, you just require the directory and register the routes property with the server. It will have all defined routes in the array from all the files.

var routes = require('./routes');
server.route(routes.routes);

This module uses the debug module for logging output. To see logging output, set

DEBUG=require-hapiroutes*

Defining Routes

In your route file you can define the routes in a few ways.

Export a property called routes on your module that is an array of HAPI route config objects. Other exports will still be available from the module if you use this way of loading routes (be careful not to overwrite the object after you set the routes property as it will be lost)

Example of routes property:

  module.exports.routes = [
    {
      method : 'GET',
      path : '/route1',
      handler : routeHandler1,
      config : {
        description: 'my route description',
        notes: 'Important stuff to know about this route',
        tags : ['app']
      }
    },
    {
      method : 'GET',
      path : '/route2',
      handler : routeHandler2,
      config : {
        description: 'my route description',
        notes: 'Important stuff to know about this route',
        tags : ['app']
      }
    }
  ];

Or, you can set the route object as your export (must have path and handler properties)

Example:

  module.exports =
  {
    method : 'GET',
    path : '/route1',
    handler : routeHandler1,
    config : {
      description: 'my route description',
      notes: 'Important stuff to know about this route',
      tags : ['app']
    }
  };

Or, you can set an array of them

module.exports = [
{
  method : 'GET',
  path : '/route3',
  handler : routeHandler3,
  config : {
    description: 'my route description',
    notes: 'Important stuff to know about this route',
    tags : ['app']
  }
},
{
  method : 'GET',
  path : '/route4',
  handler : routeHandler4,
  config : {
    description: 'my route description',
    notes: 'Important stuff to know about this route',
    tags : ['app']
  }
}
];

If you don't do either of these, it will just do the normal module loading stuff for it. Also, you can mix and match between and they will all get loaded in the end.

Release History

  • 1.0.0 Updated debug dependency and moved version number
  • 0.1.9 One more update to clean up the handler checks
  • 0.1.8 Updated loading to allow handler to be in the config section
  • 0.1.7 Added test for loading regular modules that don't have routes in them
  • 0.1.6 Updated loader to also look for the module export to be an array of route objects
  • 0.1.5 Got build running test on Travis.ci
  • 0.1.4 Added the build indicator to the readme
  • 0.1.3 Updated the package to have the tests and update the readme
  • 0.1.2 Added tests for the package
  • 0.1.1 Readme corrections for npm
  • 0.1.0 Initial release