Hapi Framework
Hapi.js is a Node framework that helps you build scalable web applications, HTTP-proxy applications, APIs, and more. Hapi offers a robust plugin system, a comprehensive and integrated authentication architecture, end-to-end high security, and more.
This example illustrates the steps to build a simple Node.js app with the Hapi framework. This application will then be bundled and associated with an AppSail service, and deployed to the console.
-
Create a new folder in your local system for the Hapi app.
-
Navigate to the directory from your terminal and initialize Node with the following command:
copynpm init
Follow the steps in the terminal and provide the required details.
- Add the Hapi module to your application using npm by executing the command:
copynpm install --save @hapi/hapi
- You can now add the logic you require in the application’s code in the main file. Given below is a sample code for a basic “Hello World” program.
copy'use strict'; const Hapi = require('@hapi/hapi'); const init = async () => { const server = Hapi.server({ port: process.env.X_ZOHO_CATALYST_LISTEN_PORT | 9000, host: '0.0.0.0' }); server.route({ method: 'GET', path: '/', handler: (request, h) => { return 'Hello World!'; } }); await server.start(); console.log('Server running on %s', server.info.uri); }; process.on('unhandledRejection', (err) => { console.log(err); process.exit(1); }); init();
-
You can now initialize an AppSail service in the same directory from the CLI or add it in an existing project directory. The app’s source must be your application’s directory. Provide the following value while initializing the app service:
Stack: Node16
-
Ensure the main file, configuration files, and the node modules are present in the build directory you specify during initialization. Catalyst will automatically ZIP your app files during deployment to the remote console.
-
Deploy the app service to the console.
-
You can then configure the startup command given below from the console:
copynode index.js
You can also configure this in the app-config.json file before deploying.
Access the deployed app service from its endpoint URL.
Last Updated 2023-12-14 16:25:23 +0530 +0530
Yes
No
Send your feedback to us