DEV Community

Cover image for Fastify on Azure Web App is super straightforward
Giorgio Boa for This is Learning

Posted on • Updated on

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


Enter fullscreen mode Exit fullscreen mode

Initialize npm



npm init -y


Enter fullscreen mode Exit fullscreen mode

Install fastify



npm i fastify


Enter fullscreen mode Exit fullscreen mode

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"
  }
}


Enter fullscreen mode Exit fullscreen mode

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();


Enter fullscreen mode Exit fullscreen mode

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

FinalResult


πŸŽ‰ Wow! We deployed together how to create a server locally with Fastify and we deployed it via Azure πŸ‘

You canΒ follow me on Twitter, where I'm posting or retweeting interesting articles.

I hope you enjoyed this article, don't forget to give ❀️.
Bye πŸ‘‹

Top comments (1)

Collapse
 
alexweininger profile image
Alex Weininger

Nice tutorial and write up!