Auto-load routes, methods, handlers, and decorators from directories using glob patterns.
- Node.js >= 18
@hapi/hapi^21
npm install @ar4mirez/hapi-octopusconst Hapi = require('@hapi/hapi');
const init = async () => {
const server = new Hapi.Server({ host: 'localhost', port: 3000 });
await server.register({
plugin: require('@ar4mirez/hapi-octopus'),
options: {
methods: { cwd: `${process.cwd()}/lib/methods` },
handlers: { cwd: `${process.cwd()}/lib/handlers` },
routes: { cwd: `${process.cwd()}/lib/routes` },
decorators: { cwd: `${process.cwd()}/lib/decorators` }
}
});
await server.start();
console.log(`Server running at: ${server.info.uri}`);
};
init();Each top-level key (methods, handlers, routes, decorators) is optional and accepts the following configuration:
| Option | Type | Default | Description |
|---|---|---|---|
cwd |
string | process.cwd() |
Directory to scan for files |
pattern |
string | '*.js' |
Glob pattern to match files |
Filenames are converted to camelCase and used as the namespace key when registering with the server.
Each export becomes a named method registered under server.methods.<camelCaseFilename>.<exportName>.
// lib/methods/math_utils.js
exports.multiply = {
name: 'multiply',
method: (a, b) => a * b,
options: { cache: { expiresIn: 60000 } }
};
exports.add = {
name: 'add',
method: (a, b) => a + b,
options: {}
};
// Registered as:
// server.methods.mathUtils.multiply(3, 4) // => 12
// server.methods.mathUtils.add(1, 2) // => 3Each export should contain a routes array of standard hapi route objects.
// lib/routes/health.js
exports.status = {
routes: [
{
method: 'GET',
path: '/status',
handler: async (request, h) => h.response({ ok: true })
}
]
};Each export is a named handler function registered via server.decorate('handler', ...).
// lib/handlers/file_upload.js
exports.upload = function (route, options) {
return async (request, h) => {
// handle file upload
return h.response({ uploaded: true });
};
};
// Use in routes:
// handler: { upload: { dest: '/tmp' } }Each export configures a server, request, or toolkit decoration.
// lib/decorators/helpers.js
exports.formatDate = {
decorate: 'server', // 'server' | 'request' | 'toolkit'
name: 'formatDate',
method: function (date) {
return new Date(date).toISOString();
}
};
// Registered as: server.formatDate(new Date())ISC © Angel Ramirez