Deploy Express.js to Azure App Service using Visual Studio Code
Deploy your Express.js app to Azure with the Visual Studio Code extension for Azure App Service extension. To accomplish this goal:
- Create Express.js app
- Create a web app resource to host app
- Deploy app to resource
- View remote logs locally
1. Set up your development environment
- Create a free Azure subscription
- Install Node.js 14+ and npm
- Install Visual Studio Code and use the following extensions:
2. Sign in to Azure
If you already use the Azure service extensions, you should already be logged in and can skip this step.
Once you've installed an extension in Visual Studio Code, you need to sign into your Azure account.
In Visual Studio Code, select the Azure explorer icon, then select Sign in to Azure, and follow the prompts.
After signing in, verify that the email address of your Azure account appears in the Status Bar and your subscription(s) appears in the Azure explorer:
If you see the error "Cannot find subscription with name [subscription ID]", this may be because you are behind a proxy and unable to reach the Azure API. Configure
HTTPS_PROXY environment variables with your proxy information in your terminal:
3. Create a local Express.js app
Create and run an Express.js app by cloning an Azure sample repository.
At a terminal command prompt, go to the location where you want to create the app folder.
Use the following base command with git to clone the repository, change into the repository folder named
myexpressapp, then install the npm dependencies.
git clone https://github.com/Azure-Samples/js-e2e-express-server.git myexpressapp && \ cd myexpressapp && \ npm install
4. Run your local Express.js app
Start the server:
Test the app by opening a browser to
http://localhost:3000. The site should appear as follows:
Press Ctrl+C in the terminal to stop the server.
5. Create App service resource in Visual Studio Code
Use Ctrl+Shift+P to open the command palette.
create webthen select Azure App Service: Create New Web App...Advanced.
You use the advanced command to have full control over the deployment including resource group, App Service Plan, and operating system rather than use Linux defaults.
Respond to the prompts as follows:
- Select your Subscription account.
- For Enter a globally unique name, enter a name that's unique across all of Azure. Use only alphanumeric characters ('A-Z', 'a-z', and '0-9') and hyphens ('-')
- Select Create new resource group and provide a name like
- Select the Linux operating system.
- Select Create a new App Service plan, provide a name like
AppServiceTutorial-plan, and select the F1 Free pricing tier.
- Select Skip for now for the Application Insights resource.
- Select a location near you.
After a short time, VS Code notifies you that creation is complete. Close the notification with the X button:
With the web app in place, you next instruct VS Code to deploy your code from the local Git repo. Select the Azure icon to open the Azure App Service explorer, expand your subscription node, right-click the name of the web app you just created, and select Configure Deployment Source.
When prompted, select LocalGit.
Select the blue up arrow icon to deploy your code to Azure:
At the prompts, select the myexpressapp folder, select your subscription account again and then select the name of the web app created earlier.
When deploying to Linux, select Yes when prompted to update your configuration to run
npm installon the target server.
When prompted with Always deploy the workspace "nodejs-docs-hello-world" to (app name)", select Yes. This tells VS Code to automatically target the same App Service web app with subsequent deployments.
Once deployment is complete, select Browse Website in the prompt to view your freshly deployed web app. The browser should display "Hello World!"
7. Stream remote service logs in Visual Studio Code
View (tail) any output that the running app generates through calls to
console.log. This output appears in the Output window in Visual Studio Code.
In the Azure App Service explorer, right-click the app node and choose Start Streaming Logs.
When prompted, choose to enable logging and restart the application.
Once the app is restarted, the VS Code Output window opens with a connection to the log stream that shows output.
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).
Refresh the web page a few times in the browser to see additional log output.
8. Make changes and redeploy
Make a small change to the app. Change
Welcome to Expressto
Welcome to Express with Visual Studio Code.
Right-click your app service from the list of App services in the App service extension, then select Deploy to Web App....
9. Clean up resources
When you want to clean up the resources, right-click on the App service in the Visual Studio Code's App Service extension, then select Delete.
Congratulations, you've successfully completed this walkthrough! You're ready to check out the other Azure extensions.
You can install all the Azure extensions together by installing the Node for Azure extension pack.
To learn more about working with Azure using Node.js, visit the resources below:
- Azure for Node.js developer center.
- Azure Cosmos DB bindings for Azure Functions 2.x
- Blog Post: Creating CRUD application with Azure Functions & MongoDB - Dev.to
- Video: Migrating Your API to Serverless using Azure Functions - YouTube
- Official YouTube Channel – Azure Functions