Edit

Deploy a Node.js web app in Azure

In this quickstart, you learn how to create and deploy your first Node.js (Express) web app to Azure App Service. App Service supports various versions of Node.js on both Linux and Windows.

The Azure CLI steps in this quickstart use the paid Premium v3 tier. The Visual Studio Code and Azure portal steps show how to use the Free tier.

This video shows you how to deploy a Node.js web app in Azure.

The steps in the video are also described in the following sections.

Set up your initial environment

  • Have an Azure account with an active subscription. If you don't have one, you can create an account for free.
  • Install Node.js LTS. Run the command node --version to verify that Node.js is installed.
  • Install Azure CLI, which you can use to run commands in a shell to create and configure Azure resources.
  • Have an Azure account with an active subscription. If you don't have one, you can create an account for free.
  • Install Node.js LTS. Run the command node --version to verify that Node.js is installed.
  • Have an FTP client, such as FileZilla, to transfer files to App Service.

Create your Node.js application

In this step, you create a basic Node.js application and ensure it runs on your computer.

Tip

If you already completed the Node.js tutorial, you can skip ahead to Deploy to Azure.

  1. Create a Node.js application by using the express application generator, which is installed by default with Node.js and npm.

    npx express-generator myExpressApp --view ejs
    

    If this is the first time you've installed the generator, npx asks you to agree to the installation.

  2. Change to the application's directory and install the npm packages.

    cd myExpressApp && npm install
    
  3. Update dependencies to the most secure version.

    npm audit fix --force
    
  4. Start the development server with debug information.

    DEBUG=myexpressapp:* npm start
    
  5. In a browser, navigate to http://localhost:3000. You should see something like this:

    Screenshot of an Express application running in a browser.

Deploy to Azure

Before you continue, ensure that you have all the prerequisites installed and configured.

Note

For your Node.js application to run in Azure, it needs to listen on the port provided by the PORT environment variable. In your generated Express app, this environment variable is already used in the startup script bin/www. (Search for process.env.PORT.)

Sign in to Azure

  1. In the terminal, ensure you're in the myExpressApp directory, and then start Visual Studio Code with the following command:

    code .
    
  2. In Visual Studio Code, in the Activity Bar, select the Azure logo.

  3. Ensure that you're signed in with your Azure account.

Configure the App Service app and deploy code

  1. Right-click App Services and select Create new Web App. A Linux container is used by default.

  2. Type a globally unique name for your web app and select Enter. The name must be unique across all of Azure and use only alphanumeric characters (A-Z, a-z, and 0-9) and hyphens (-).

  3. In Select a runtime stack, select the Node.js version you want. Node 24 LTS is recommended.

  4. In Select a pricing tier, select Free (F1) and wait for the resources to be created in Azure.

  5. In the popup Always deploy the workspace "myExpressApp" to <app-name>", select Yes. Doing so ensures that, as long as you're in the same workspace, Visual Studio Code deploys to the same App Service app each time.

    While Visual Studio Code creates the Azure resources and deploys the code, it shows progress notifications.

  6. After deployment is complete, select Browse Website in the notification popup. The browser should display the Express default page.

In the terminal, ensure you're in the myExpressApp directory. Replace <app-name> with a globally unique name, then create the resource group, App Service plan, and app. Set SCM_DO_BUILD_DURING_DEPLOYMENT so App Service installs the dependencies and builds the app when you deploy the source archive.

az group create --name myResourceGroup --location eastus
az appservice plan create --name myAppServicePlan --resource-group myResourceGroup --sku P0v3 --is-linux
az webapp create --name <app-name> --resource-group myResourceGroup --plan myAppServicePlan --runtime "NODE:24-lts"
az webapp config appsettings set --name <app-name> --resource-group myResourceGroup --settings SCM_DO_BUILD_DURING_DEPLOYMENT=true
  • If the az command isn't recognized, ensure you have the Azure CLI installed as described in Set up your initial environment.
  • Replace <app-name> with a name that's unique across all of Azure (valid characters are a-z, 0-9, and -).
  • The --sku P0v3 argument selects the paid Premium v3 pricing tier. Replace eastus with your preferred region; list available regions with az account list-locations.
  • --is-linux selects a Linux plan. Omit it for a Windows plan, as shown in the Windows tab.

Create a ZIP archive of the files in myExpressApp, not the enclosing directory. On Linux or macOS, run zip -r ../app.zip . -x '*/node_modules/*' '*/.git/*' '*/.env*' from myExpressApp. On Windows, run Compress-Archive -Path (Get-ChildItem -Force -Name | Where-Object { $_ -notin @('node_modules', '.git') -and $_ -notlike '.env*' }) -DestinationPath ..\app.zip in PowerShell from myExpressApp. Then deploy the archive:

az webapp deploy --resource-group myResourceGroup --name <app-name> --src-path ../app.zip --type zip

Browse to https://<app-name>.azurewebsites.net to see the app.

Create Azure resources

  1. Sign in to the Azure portal.

  2. To start creating a Node.js app, browse to https://portal.azure.com/#create/Microsoft.WebSite.

  3. In the Basics tab, under Project Details, ensure the correct subscription is selected and then select Create new to create a resource group. Type myResourceGroup for the name.

    Screenshot of the Project Details section showing where you select the Azure subscription and the resource group for the web app.

  4. Under Instance details, type a globally unique name for your web app and select Code. Select Node 24 LTS in Runtime stack, an Operating System, and a Region you want to serve your app from.

    Screenshot of the Instance Details section.

  5. Under Pricing plans, select Create new to create an App Service plan. Type myAppServicePlan for the name. To change to the Free tier, select Free F1 in the Pricing plan list.

    Screenshot of the App Service Plan section.

  6. Select the Review + create button at the bottom of the page.

    Screenshot showing the Review and create button at the bottom of the page

  7. After validation runs, select the Create button at the bottom of the page.

  8. After deployment is complete, select Go to resource.

    Screenshot showing the Go to resource button.

Get FTPS credentials

Azure App Service supports two types of credentials for FTP/S deployment. These credentials aren't the same as your Azure subscription credentials. In this section, you get the application-scope credentials to use with your FTP client.

  1. From the App Service app page, select Deployment Center under Deployment in the sidebar menu, and then select the FTPS Credentials tab.

    Screenshot that shows the FTPS deployment credentials tab.

  2. Open your FTP client and navigate to your myExpressApp folder.

  3. From the FTPS credentials tab, copy the FTPS endpoint, Username, and Password into your FTP client.

    Screenshot of the FTPS connection details.

  4. Select Connect in your FTP client.

Deploy files with FTPS

  1. Copy all files and directories files to the /site/wwwroot directory in Azure.

    Screenshot of the WWW root directory.

  2. Browse to your app's URL to verify the app is running properly.

Redeploy updates

You can deploy changes to this app by making edits in Visual Studio Code, saving your files, and then redeploying to your Azure app. For example:

  1. From the sample project, open views/index.ejs and change

    <p>Welcome to <%= title %></p>
    

    to

    <p>Welcome to Azure</p>
    
  1. In the App Service explorer, select the Deploy to Web App icon again, and confirm by selecting Deploy again.

  2. Wait for deployment to complete, then select Browse Website in the notification popup. You should see that the Welcome to Express message was changed to Welcome to Azure.

  1. Save your changes, re-create ../app.zip from the app files as described in Deploy to Azure, and redeploy:

    az webapp deploy --resource-group myResourceGroup --name <app-name> --src-path ../app.zip --type zip
    

    Replace <app-name> with the name you used when you created the web app.

  2. Once deployment is complete, refresh the webpage. You should see that the Welcome to Express message was changed to Welcome to Azure.

  1. Save your changes, then redeploy the app using your FTP client.

  2. Once deployment is complete, refresh the webpage. You should see that the Welcome to Express message was changed to Welcome to Azure.

Stream logs

You can stream log output (calls to console.log()) from the Azure app directly in the Visual Studio Code output window.

  1. In Resources in Visual Studio Code, right-click the app node and then select Start Streaming Logs.

    Screenshot of the option to Start Streaming Logs.

  2. If asked to restart the app, select Yes. Once the app is restarted, the Visual Studio Code output window opens with a connection to the log stream.

  3. After a few seconds, the output window shows a message indicating that you're connected to the log-streaming service. You can generate more output activity by refreshing the page in the browser.

    Connecting to log stream...
    2020-03-04T19:29:44  Welcome, you are now connected to log-streaming service. The default timeout is 2 hours.
    Change the timeout with the App Setting SCM_LOGSTREAM_TIMEOUT (in seconds).
    

You can access the console logs generated from inside the app and the container in which it runs. Logs include any output generated by calls to console.log().

To stream logs, run the az webapp log tail command:

  • Enable logging first: on Linux, run az webapp log config --resource-group myResourceGroup --name <app-name> --docker-container-logging filesystem; on Windows, run az webapp log config --resource-group myResourceGroup --name <app-name> --application-logging filesystem.
az webapp log tail --resource-group myResourceGroup --name <app-name>

Replace <app-name> with the name you used when you created the web app.

Refresh the app in the browser to generate console logs, which include messages describing HTTP requests to the app. If no output appears immediately, try again in 30 seconds.

To stop log streaming at any time, select Ctrl+C in the terminal.

You can access the console logs generated from inside the app and the container in which it runs. You can stream log output (calls to console.log()) from the Node.js app directly in the Azure portal.

  1. In the same App Service page for your app, use the sidebar menu to scroll to the Monitoring section and select Log stream.

    Screenshot of Log stream in Azure App service.

  2. After a few seconds, the output window shows a message indicating that you're connected to the log-streaming service. You can generate more output activity by refreshing the page in the browser.

    Connecting...
    2021-10-26T21:04:14  Welcome, you are now connected to log-streaming service.
    Starting Log Tail -n 10 of existing logs ----
    /appsvctmp/volatile/logs/runtime/81b1b83b27ea1c3d598a1cdec28c71c4074ce66c735d0be57f15a8d07cb3178e.log
    2021-10-26T21:04:08.614384810Z: [INFO]
    2021-10-26T21:04:08.614393710Z: [INFO]  # Enter the source directory to make sure the script runs where the user expects
    2021-10-26T21:04:08.614399010Z: [INFO]  cd "/home/site/wwwroot"
    2021-10-26T21:04:08.614403210Z: [INFO]
    2021-10-26T21:04:08.614407110Z: [INFO]  export NODE_PATH=/usr/local/lib/node_modules:$NODE_PATH
    2021-10-26T21:04:08.614411210Z: [INFO]  if [ -z "$PORT" ]; then
    2021-10-26T21:04:08.614415310Z: [INFO]          export PORT=8080
    2021-10-26T21:04:08.614419610Z: [INFO]  fi
    2021-10-26T21:04:08.614423411Z: [INFO]
    2021-10-26T21:04:08.614427211Z: [INFO]  node /opt/startup/default-static-site.js
    Ending Log Tail of existing logs ---
    

Clean up resources

In the preceding steps, you created Azure resources in a resource group. The steps in this quickstart put all the resources in this resource group. To clean up, you just need to remove the resource group.

  1. In the Azure extension of Visual Studio Code, in the Group By list, select Group by Resource Group.

  2. In the Resources list, expand the subscription, right-click the resource group you created earlier, and then select Delete Resource Group.

    Screenshot of the Visual Studio Code navigation to delete a resource that contains App Service resources.

  3. When prompted, confirm your deletion by entering the name of the resource group you're deleting. After you confirm, the resource group is deleted, and you see a notification when it's done.

In the preceding CLI steps, you created Azure resources in myResourceGroup.

If you don't expect to need these resources in the future, delete the resource group by running the following command:

az group delete --name myResourceGroup --no-wait

The --no-wait argument allows the command to return before the operation is complete.

You can delete the resource group, App service, and all related resources when they're no longer needed.

  1. From your App Service overview page, select the resource group you created in the Create Azure resources step.

    Screenshot of resource group in App Service overview page.

  2. From the resource group page, select Delete resource group. Confirm the name of the resource group to finish deleting the resources.

    Screenshot of Delete resource group.

Congratulations, you've successfully completed this quickstart!

Check out the other Azure extensions.

Or get them all by installing the Node Pack for Azure extension pack.