Fastify is a high-performance, highly-extensible Node.js framework with minimal overhead, that helps you create reliable and efficient applications and APIs. Fastify brings in a host of convent features, such as affordable logging, Schema support, TypeScript-readiness, and various other developer-friendly features.

This example illustrates the steps to build a simple Node.js app with the Fastify framework. This application will then be bundled and associated with an AppSail service, and deployed to the console.

  1. Create a new folder in your local system for the Fastify app.

  2. Navigate to the directory from your terminal and initialize Node with the following command:

npm init

Follow the steps in the terminal and provide the required details.

  1. Add the Fastify framework to your application using npm by executing the command:
npm install --save fastify
  1. 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.
// Require the framework and instantiate it const fastify = require('fastify')({ logger: true }) // Declare a route fastify.get('/', async (request, reply) => { return { hello: 'world' } }) // Run the server! const start = async () => { try { await fastify.listen({ port: process.env.X_ZOHO_CATALYST_LISTEN_PORT | 9000 , host: "" }) } catch (err) { fastify.log.error(err) process.exit(1) } } start()
  1. 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

  2. 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.

  3. Deploy the app service to the console.

  4. You can then configure the startup command given below from the console:

node 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