Hapi JS Adapter
This is the official adapter for hapi and neru, you can find the code for that at packages/adapter-hapi
Feature Support
Feature | Supported |
---|---|
Parameters | ✅ |
Spread Parameters | ✅ |
ALL Handler | ✅ |
Restricted ALL Handler | ❌ |
Creating your project
Neru CLI
The quickest way to get started with Neru is using the Neru CLI. This CLI has an option for the hapi adapter!
Manually
Please read the guide here to find out how to use neru on a new/existing project manually.
Here is an example what your project's main file might look like:
import { adapter } from '@nerujs/hapi';
import Hapi from '@hapi/hapi';
import { neru } from 'neru';
const server = Hapi.server({
port: 4000,
host: 'localhost',
});
await neru({
adapter,
server,
routes: 'src/routes',
});
await server.start();
console.log(`Online on ${server.info.uri}`);
You will need the following packages:
npm install @hapi/hapi neru @nerujs/hapi
Routes
Before you read how to make routes using the hapi adapter make sure you read how route files work in neru.
The hapi adapter exports the route
function and we recommend you use this for type safety, though it's not required.
Type Safe
import { route } from '@nerujs/hapi';
export const GET = route({
handler() {
return 'Hello world!';
},
});
Basic
export const GET = {
handler() {
return 'Hello world!';
},
};
Typing Manually
If you want to be type safe but not use the provided tools, here is how you can do that:
import type { ServerRoute } from '@hapi/hapi'
export const GET: Omit<ServerRoute, 'path' | 'method'> = {
handler() {
return 'Hello world!';
},
};
You can skip the Omit
type helper by using the import from neru:
/** @type {import('@nerujs/hapi').NeruHapiServerRoute} */
export const GET: NeruHapiServerRoute = {
handler() {
return 'Hello world!';
},
};