Hapi support for Node.js
Introduction
Hapi is an open-source framework for web applications. The most common use of Hapi is to build web services - see https://hapi.dev/ for more information.
Objects
This extension creates the application and operation objects:
- a ‘NodeJS Application’ object when the ‘Hapi.server’ API or a ‘Hapi.Server’ instance is found;
- a ‘NodeJS Delete Operation’ object when the ‘route’ API of a server is found, and the method key is delete;
- a ‘NodeJS Get Operation’ object when the ‘route’ API of a server is found, and the method key is get;
- a ‘NodeJS Post Operation’ object when the ‘route’ API of a server is found, and the method key is post;
- a ‘NodeJS Put Operation’ object when the ‘route’ API of a server is found, and the method key is put;
- a ‘NodeJS Patch Operation’ object when the ‘route’ API of a server is found, and the method key is patch.
| Icon | Description |
|---|---|
![]() |
NodeJS Application |
![]() |
NodeJS Delete Operation |
![]() |
NodeJS Get Operation |
![]() |
NodeJS Post Operation |
![]() |
NodeJS Put Operation |
![]() |
NodeJS Patch Operation |
Supported links
| Link Type | Caller type | Callee type |
|---|---|---|
| callLink |
|
|
| relyonLink |
|
|
Example
Take the following codes:
const Hapi = require('@hapi/hapi');
const init = async () => {
const server = Hapi.server({
port: 3000,
host: 'localhost'
});
server.route({
method: 'GET',
path: '/api',
handler: (request, h) => {
return 'Hello World!';
}
});
server.route({
method: 'POST',
path: '/api',
handler: (request, h) => {
const payload = request.payload;
return h.response(payload).code(201);
}
});
server.route({
method: 'PUT',
path: '/api/{id}',
handler: (request, h) => {
const { id } = request.params;
const payload = request.payload;
return h.response({ id, ...payload }).code(200);
}
});
server.route({
method: 'PATCH',
path: '/api/{id}',
handler: (request, h) => {
const { id } = request.params;
const payload = request.payload;
return h.response({ id, ...payload }).code(200);
}
});
server.route({
method: 'DELETE',
path: '/api/{id}',
handler: (request, h) => {
return h.response().code(204);
}
});
await server.start();
};
In this example, a NodeJS Application and five NodeJS operation objects are created. A relyOnLink is added between index application and index.js source code when ‘server’ API is found in index.js file. Five callLink are also created between the operations and handler functions corresponding.





