hapi-router icon indicating copy to clipboard operation
hapi-router copied to clipboard

Dead? Hapi 20.x Support?

Open bnbon opened this issue 3 years ago • 2 comments

Hello,

Just wondering if this is dead, and if so how I might migrate away, I wish to use hapi 20.x

I was using this;

  try {
    await server.register({
      plugin: require('hapi-router'),
      options: {
        routes: './src/components/**/*Route.js'
      }
    });
  } catch (err) {
    console.log(err);
  }

bnbon avatar Aug 29 '22 22:08 bnbon

@bonbonio

You don't really need this plugin. Check out the follow on how to handle nested routes.

https://github.com/johnmanko/catbox-redis-example

johnmanko avatar Aug 30 '22 00:08 johnmanko

Unfortunately, I am not using imports, models or typescript so that example is quite hard for me to follow...

My routes are in src/routes/ as folders with a controller file and a route file that gets loaded as per regex.

This is an example of src/routes/Audit/AuditRoute.js, there are maybe 20 folders organised like this

'use strict';

const Joi = require('joi');
const AuditCtrl = require('./auditCtrl');

module.exports = (() => {
  return [
    {
      method: 'GET',
      path: '/audit',
      handler: AuditCtrl.getAudit,
      },
    },
    {
      method: 'POST',
      path: '/audit',
      handler: AuditCtrl.createAudit,
      },
    },
  ];
})();

and this is my index.js file which powers the server;

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

const init = async () => {
  const server = await new Hapi.Server({
    host: '0.0.0.0',
    port: 4000,
    routes: {
      cors: {
        origin: envConfig.origin,
        credentials: true,
        exposedHeaders: ['x-total-count']
      }
    }
  });

  await server.register([
    require('hapi-auth-jwt2'),
    require('@hapi/inert'),
    require('@hapi/vision'),
    {
      plugin: require('hapi-swagger'),
      options: {}
    }
  ]);

  try {
    await server.register({
      plugin: require('hapi-router'),
      options: {
        routes: './src/routes/**/*Route.js'
      }
    });
  } catch (err) {
    console.log(err);
  }

  try {
    await server.start();
    console.log('Server running at:', server.info.uri);
  }
};

init();

How might I load the routes? Forgive me for the asking but I spent most of the night on this, its a codebase I am trying to modernise, but this older functionality seems quite neat.

bnbon avatar Aug 30 '22 14:08 bnbon