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

v5.1.4

Published

Mount directories containing your routes, etc. into your hapi server

Downloads

12

Readme

hapi-mount

Mount directories containing your routes, etc. into your hapi server

npm Build Status Coverage Status Dependency Status devDependency Status node

Usage

Intro

This module will allow you to organize your handlers as such:

- server/
  - ext/
    - on-post-auth.js
  - methods/
    - get-user.js
  - routes/
    - user/
      - get.js
      - post.js

A single object of the array overloads for server.ext, server.method, and server.route must be exported. An array can also be used, this module will flatten the array anyway.

For the ext and methods directories, this will require all .js files inside them.

For the routes directory, only valid HTTP verbs in lowercase (get.js, post.js, etc.) are included.

Example (just to give you an idea how modular your server will be now):

ext/on-post-auth.js

module.exports = {
  type: 'onPostAuth',
  async method(request, h) {
    // ...
  }
}

methods/get-user.js

module.exports = {
  name: 'getUsers',
  async method() {
    // ...
  }
}

routes/user/get.js

module.exports = {
  path: '/user',
  method: 'GET',
  async handler(request, h) {
    // ...
  }
}

Note: Modules with empty exports will be excluded. This is to avoid errors in development, e.g. when creating a new blank file. (Hapi will emit an error anyway for these files since they don't match the schema.) Modules that export null or undefined will also be excluded.

General usage

await server.register(
  {
    plugin: require('hapi-mount'),
    options: {
      /* cwd, routes, methods, ext */
    }
  },
  options
)

Registration options

  • cwd: string: Main server directory to look for routes, methods, and ext. Default: ".". This is resolved against the current working directory.
  • routes: string: Name of the routes directory. Default: "routes".
  • methods: string: Name of the methods directory. Default: "methods".
  • ext: string: Name of the directory for extension functions. Default: "ext"
  • bind: object: Object to bind as the context. (Plugin binds are isolated.) Optional.
  • bindParentContext: boolean: Bind to the context of the parent realm.

Path defaults

Routes

If the route object is a function, it will be transformed into the handler (handler) for the route.

If you use the format described above for specifying routes, the path and method will default to the path dirname and basename, respectively. For example, a file at foo/bar/get.js will default all of its exported objects with { path: '/foo/bar', method: 'get' }.

ext

If the ext object is a function, it will be transformed into the method property for the ext object.

If you use the kebab case version of a valid Hapi extension point as the filename, the type will default to that extension point. For example, a file at on-pre-response.js will default to { type: 'onPreResponse' }.

Methods

If the method object is a function, it will be transformed into the method property for the method object.

If you don't specify the name for the method, the name will default to the camel case version of the basename of the file. For example, a file at get-database.js will default to { name: 'getDatabase' }

All of these are optional!

If this behavior isn't your style, simply specify everything in the schema.

Versions

  • <=4 - hapi v16 compatible
  • >=5 - hapi v17 compatible

See also

Oops. Looks like modules of this kind have been done already. Here they are for reference: