Fastify on Azure Web App is super straightforward

Today I’ll show you step by step how easy it is to deploy a Fastify server on Azure Function.

Steps to create a Fastify server

Project folder creation

mkdir azure-fastify && cd azure-fastify

Initialize npm…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Giorgio Boa

Today I'll show you step by step how easy it is to deploy a Fastify server on Azure Function.

Steps to create a Fastify server

Project folder creation

mkdir azure-fastify && cd azure-fastify

Initialize npm

npm init -y

Install fastify

npm i fastify

package.json

You should find this file inside your project, it was created by npm

{
  "name": "azure-fastify",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "start": "node index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "fastify": "^4.10.2"
  }
}

I added the script to launch the Fastify server "start": "node index.js" and "type": "module"

index.js

Let's create our code for the Fastify server

import Fastify from 'fastify';
const fastify = Fastify({
  logger: true,
});

fastify.get('/', async (request, reply) => {
  return { randomNumber: Math.random() };
});

const start = async () => {
  try {
    const port = process.env.port || 8080;
    await fastify.listen({ port, host: '0.0.0.0' }, () =>
      console.log('SERVER LISTENING AT PORT : ' + port)
    );
  } catch (err) {
    fastify.log.error(err);
    process.exit(1);
  }
};

start();

Here we are creating our Fastify server which responds to the port specified in the environment variables or as an alternative to 8080.
It exposes a JSON with a random number inside.

Local Run

Through the npm run start command we can test our server locally.
Here is the local result:

LocalResponse

Steps to deploy on Azure

Install Azure App Service extension

AzureExtension

Once the extension is installed we will have this new icon in VSCode

AzureIcon

Login into Azure

Login

By pressing on the login button, we are taken to the browser to log in with our Azure account.

You need to have an Azure account, free plan works well for this example.

LoginBrowser

Once logged in you can go back to VSCode

Logged

Creation of a new Web App

We use the following command to create our application on Azure

WebApp

We need to enter the name of our application

AppName

We need to select Node 18 LTS

Node18LTS

Let's select Free pricing

Free

The application will be created automatically

Creation

Once the application has been created, press the Deploy button

AzureDeploy

Let's select the project folder

Folder

InDeploy

After the deployment let's see our app on Azure

Success

AppAzureRun

🎉 Wow! We deployed together how to create a server locally with Fastify and we deployed it via Azure 👏

I hope you enjoyed this article, don't forget to give ❤️.
Bye đź‘‹


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Giorgio Boa


Print Share Comment Cite Upload Translate Updates
APA

Giorgio Boa | Sciencx (2022-12-17T13:16:52+00:00) Fastify on Azure Web App is super straightforward. Retrieved from https://www.scien.cx/2022/12/17/fastify-on-azure-web-app-is-super-straightforward/

MLA
" » Fastify on Azure Web App is super straightforward." Giorgio Boa | Sciencx - Saturday December 17, 2022, https://www.scien.cx/2022/12/17/fastify-on-azure-web-app-is-super-straightforward/
HARVARD
Giorgio Boa | Sciencx Saturday December 17, 2022 » Fastify on Azure Web App is super straightforward., viewed ,<https://www.scien.cx/2022/12/17/fastify-on-azure-web-app-is-super-straightforward/>
VANCOUVER
Giorgio Boa | Sciencx - » Fastify on Azure Web App is super straightforward. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/12/17/fastify-on-azure-web-app-is-super-straightforward/
CHICAGO
" » Fastify on Azure Web App is super straightforward." Giorgio Boa | Sciencx - Accessed . https://www.scien.cx/2022/12/17/fastify-on-azure-web-app-is-super-straightforward/
IEEE
" » Fastify on Azure Web App is super straightforward." Giorgio Boa | Sciencx [Online]. Available: https://www.scien.cx/2022/12/17/fastify-on-azure-web-app-is-super-straightforward/. [Accessed: ]
rf:citation
» Fastify on Azure Web App is super straightforward | Giorgio Boa | Sciencx | https://www.scien.cx/2022/12/17/fastify-on-azure-web-app-is-super-straightforward/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.