Skip to content

ar4mirez/hapi-safe-route

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

hapi-safe-route

npm version CI License: ISC

Register routes in a safer way, avoiding server crash on duplicate routes.

Requirements

  • Node.js >= 18
  • @hapi/hapi ^21

Installation

npm install @ar4mirez/hapi-safe-route

Usage

const Hapi = require('@hapi/hapi');

const init = async () => {
  const server = new Hapi.Server({ host: 'localhost', port: 3000 });

  await server.register(require('@ar4mirez/hapi-safe-route'));
  await server.initialize();

  const { safeRoute } = server.plugins['@ar4mirez/hapi-safe-route'];

  // Register routes safely — no crash on duplicates
  const registered = await safeRoute([
    {
      method: 'GET',
      path: '/hello',
      handler: (request, h) => h.response({ hello: 'world' })
    },
    {
      method: 'POST',
      path: '/echo',
      handler: (request, h) => h.response(request.payload)
    }
  ]);

  console.log(`Registered ${registered.length} new routes`);

  // Calling safeRoute again with the same routes silently skips them
  // and returns an empty array instead of throwing
  const duplicates = await safeRoute([
    { method: 'GET', path: '/hello', handler: (request, h) => h.response('duplicate') }
  ]);
  console.log(duplicates.length); // 0

  await server.start();
  console.log(`Server running at: ${server.info.uri}`);
};

init();

API

safeRoute(routes)

Asynchronous function exposed via server.plugins['@ar4mirez/hapi-safe-route'].safeRoute.

Parameter Type Description
routes array Array of hapi route configuration objects

Returns: Promise<Array> — array of newly registered route objects. Returns an empty array if all provided routes already exist (no duplicates throw).

Behavior:

  • Checks all existing server routes before attempting registration
  • Silently skips any route whose method + path combination already exists
  • Only registers and returns routes that are genuinely new
  • Never throws due to duplicate route registration

License

ISC © Angel Ramirez

About

Register routes in Hapi on the fly.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors