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-routing

v2.0.0

Published

Simple route and controller management for hapijs

Downloads

4

Readme

HapiRouting

This plugins adds a pretty simple and easy to use routing & controllers in hapi

Installation

$ npm install --save hapi-routing

Usage

You need to create a route file, like this:

// routes.js
'use strict';

module.exports = table => table
  .get('/first-url', 'first#index')
  .post('/second-url', 'second#index')
  .match('/third-url/{param}', 'third#anotherAction', 'get', 'put', 'patch')
;

And register the module in the hapi server using that route:

hapiRouting = require('hapi-routing');
routes = require('./routes');
// hapi initialization code

hapi.register(hapiRouting(routes));

// additional hapi code, and hapi.start call
// controllers/first
module.exports = class FirstController {
  index(request, reply) {
    reply('OK!');
  }
}

Api

hapiRouting(routes, [controllerPath])

  • routes must be a function that will receive a RouteTable
  • controllerPath is the path to the controllers (called with require.main.require). You can also pass an absolute path. The default value is ./controllers

It returns the plugin configuration to load it with hapi.register

RouteTable

This is the object passed to the routes function. It has several functions, which should be chained to specify the routes (they all return the RouteTable object).

[delete|get|head|patch|post|put](url, handler)

Utility methods to define a route with the specified method.

  • url is a url in the same format as hapi default routes
  • handler is a string composed of 'controllerName#actionName'

match(url, handler, ...methods)

Defines a route that replies to more than one method

  • url is a url in the same format as hapi default routes
  • handler is a string composed of 'controllerName#actionName'
  • method a string with the method name

Controller

It's a simple class that must have defined a method for every action in your routing table. Each action receives hapi's request and reply objects as parameters.

Validation

If you want to add hapi validation to an action, you need to create a static method in your controller called actionNameValidation that will receive a method (in string, uppercase) and should return the Joi validation configuration.

// controllers/third
const Joi = require('joi');

module.exports = class ThirdController {
  anotherAction(request, reply) {
    reply('OK!');
  }

  static anotherActionValidation(method) {
    switch (method) {
      case 'PUT':
      case 'PATCH':
        return {
          params: {
            param: Joi.string().required()
          }
        };
    }
  }
}