Get started with Node.js web apps in Azure App Service
This tutorial shows how to create a simple Node.js application and deploy it to a web app in Azure App Service from a command line environment such as cmd.exe or bash. The instructions in this tutorial can be followed on any operating system that is capable of running Node.js.
Prerequisites
- Node.js (Click here to install)
- Bower (Click here to install)
- Yeoman (Click here to install)
- Git (Click here to install)
- Azure CLI (Click here to install)
- A Microsoft Azure account. If you don't have an account, you can sign up for a free trial or activate your Visual Studio subscriber benefits.
Create and deploy a simple Node.js web app
Open the command-line terminal of your choice and install the Express generator for Yeoman.
npm install -g generator-express
CD
to a working directory and generate an express app using the following syntax:yo express
Choose the following options when prompted:
? Would you like to create a new directory for your project?
Yes
? Enter directory name
{appname}
? Select a version to install:
MVC
? Select a view engine to use:
Jade
? Select a css preprocessor to use (Sass Requires Ruby):
None
? Select a database to use:
None
? Select a build tool to use:
GruntCD
to the root directory of your new app and start it to make sure it runs in your development environment:npm start
In your browser, navigate to http://localhost:3000 to make sure that you can see the Express home page. Once you've verified your app runs properly, use
Ctrl-C
to stop it.Log in to Azure like so (you need Azure CLI for this):
azure login
Follow the prompt to continue the login in a browser with a Microsoft account that has your Azure subscription.
Make sure you're still in the root directory of your app, then create the App Service app resource in Azure with a unique app name with the next command; for example: http://{appname}.azurewebsites.net
azure site create --git {appname}
Follow the prompt to select an Azure region to deploy to. If you've never set up Git/FTP deployment credentials for your Azure subscription, you'll also be prompted to create them.
Open the ./config/config.js file from the root of your application and change the production port to
process.env.port
; yourproduction
property in theconfig
object should look like the following example.production: { root: rootPath, app: { name: 'express1' }, port: process.env.port, }
This lets your Node.js app respond to web requests on the default port that iisnode listens.
Save your changes, then use git to deploy your app to Azure:
git add . git commit -m "{your commit message}" git push azure master
The Express generator already provides a .gitignore file, so your
git push
won't consume bandwidth trying to upload the node_modules/ directory.Finally, launch your live Azure app in the browser:
azure site browse
You should now see your Node.js web app running live in Azure App Service.
Update your Node.js web app
To make updates to your Node.js web app running in App Service, just run git add
, git commit
, and git push
like you did when you first deployed your web app.
How App Service deploys your Node.js app
Azure App Service uses iisnode to run Node.js apps. The Azure CLI and the Kudu engine (Git deployment) work together to give you a streamlined experience when you develop and deploy Node.js apps from the command line.
azure site create --git
recognizes the common Node.js pattern of server.js or app.js and creates an iisnode.yml in your root directory. You can use this file to customize iisnode.At
git push azure master
, Kudu automates the following deployment tasks:- If package.json is in the repository root, run
npm install --production
. - Generate a Web.config for iisnode that points to your start script in package.json (e.g. server.js or app.js).
- Customize Web.config to ready your app for debugging with Node-Inspector.
- If package.json is in the repository root, run
Use a Node.js framework
If you use a popular Node.js framework, such as Sails.js or MEAN.js to develop apps, you can deploy those to App Service. Popular Node.js frameworks have their specific quirks, and their package dependencies keep getting updated. However, App Service makes the the stdout and stderr logs available to you, so you can know exactly what's happening with your app and make changes accordingly. For more information, see Get stdout and stderr logs from iisnode.
The following tutorials will show you how to work with a specific framework in App Service:
- Deploy a Sails.js web app to Azure App Service
- Create a Node.js chat application with Socket.IO in Azure App Service
- How to use io.js with Azure App Service Web Apps
Use a specific Node.js engine
In your typical workflow, you can tell App Service to use a specific Node.js engine as you normally would in package.json. For example:
"engines": { "node": "5.5.0" },
The Kudu deployment engine determines which Node.js engine to use in the following order:
- First, look at iisnode.yml to see if
nodeProcessCommandLine
is specified. If yes, then use that. - Next, look at package.json to see if
"node": "..."
is specified in theengines
object. If yes, then use that. - Choose a default Node.js version by default.
Get stdout and stderr logs from iisnode
To read iisnode logs, use the following steps.
Open the iisnode.yml file that the Azure CLI provides.
Set the two following parameters:
loggingEnabled: true logDirectory: iisnode
Together, they tell iisnode in App Service to put its stdout and stderror output in the D:\home\site\wwwroot**iisnode** directory.
Save your changes, then push your changes to Azure with the following Git commands:
git add . git commit -m "{your commit message}" git push azure master
iisnode is now configured. The next steps show you how to access these logs.
In your browser, access the Kudu debug console for your app, which is at:
https://{appname}.scm.azurewebsites.net/DebugConsole
Note that this URL differs from the web app URL by the addition of ".scm." to the DNS name. If you omit that addition to the URL, you will get a 404 error.
Navigate to D:\home\site\wwwroot\iisnode
Click the Edit icon for the log you want to read. You can also click Download or Delete if you want.
Now you can see the log to help you debug your App Service deployment.
Debug your app with Node-Inspector
If you use Node-Inspector to debug your Node.js apps, you can use it for your live App Service app. Node-Inspector is preinstalled in the iisnode installation for App Service. And if you deploy through Git, the auto-generated Web.config from Kudu already contains all the configuration you need to enable Node-Inspector.
To enable Node-Inspector, follow these steps:
Open iisnode.yml at your repository root and specify the following parameters:
debuggingEnabled: true debuggerExtensionDll: iisnode-inspector.dll
Save your changes, then push your changes to Azure with the following Git commands:
git add . git commit -m "{your commit message}" git push azure master
Now, just navigate to your app's start file as specified by the start script in your package.json, with /debug added to the URL. For example,
http://{appname}.azurewebsites.net/server.js/debug
Or,
http://{appname}.azurewebsites.net/app.js/debug
More resources
- Specifying a Node.js version in an Azure application
- Best practices and troubleshooting guide for Node.js applications on Azure
- How to debug a Node.js web app in Azure App Service
- Using Node.js Modules with Azure applications
- Azure App Service Web Apps: Node.js
- Node.js Developer Center
- Get started with web apps in Azure App Service
- Exploring the Super Secret Kudu Debug Console