Skip to content

Commit cd6cd4c

Browse files
Merge pull request #285933 from v-albemi/configure-node
Freshness - Azure App Service
2 parents 6757176 + 7ef9d56 commit cd6cd4c

File tree

1 file changed

+32
-35
lines changed

1 file changed

+32
-35
lines changed

articles/app-service/configure-language-nodejs.md

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ description: Learn how to configure a Node.js app in the native Windows instance
44
ms.custom: devx-track-js, devx-track-azurecli, linux-related-content
55
ms.devlang: javascript
66
# ms.devlang: javascript, devx-track-azurecli
7-
ms.topic: article
8-
ms.date: 01/21/2022
7+
ms.topic: how-to
8+
ms.date: 09/13/2024
99
author: msangapu-msft
1010
ms.author: msangapu
1111
zone_pivot_groups: app-service-platform-windows-linux
1212
---
1313

1414
# Configure a Node.js app for Azure App Service
1515

16-
Node.js apps must be deployed with all the required NPM dependencies. The App Service deployment engine automatically runs `npm install --production` for you when you deploy a [Git repository](deploy-local-git.md), or a [Zip package](deploy-zip.md) [with build automation enabled](deploy-zip.md#enable-build-automation-for-zip-deploy). If you deploy your files using [FTP/S](deploy-ftp.md), however, you need to upload the required packages manually.
16+
Node.js apps must be deployed with all the required npm dependencies. The App Service deployment engine automatically runs `npm install --production` for you when you deploy a [Git repository](deploy-local-git.md), or when you deploy a [Zip package](deploy-zip.md) [with build automation enabled](deploy-zip.md#enable-build-automation-for-zip-deploy). If you deploy your files using [FTP/S](deploy-ftp.md), however, you need to upload the required packages manually.
1717

1818
This guide provides key concepts and instructions for Node.js developers who deploy to App Service. If you've never used Azure App Service, follow the [Node.js quickstart](quickstart-nodejs.md) and [Node.js with MongoDB tutorial](tutorial-nodejs-mongodb-app.md) first.
1919

@@ -64,7 +64,7 @@ az webapp config appsettings set --name <app-name> --resource-group <resource-gr
6464
> [!NOTE]
6565
> This example uses the recommended "tilde syntax" to target the latest available version of Node.js 16 runtime on App Service.
6666
>
67-
>Since the runtime is regularly patched and updated by the platform it's not recommended to target a specific minor version/patch as these are not guaranteed to be available due to potential security risks.
67+
>Since the runtime is regularly patched and updated by the platform, we don't recommend that you target a specific minor version/patch as these are not guaranteed to be available due to potential security risks.
6868
6969
> [!NOTE]
7070
> You should set the Node.js version in your project's `package.json`. The deployment engine runs in a separate process that contains all the supported Node.js versions.
@@ -92,13 +92,13 @@ Your Node.js app needs to listen to the right port to receive incoming requests.
9292

9393
::: zone pivot="platform-windows"
9494

95-
In App Service on Windows, Node.js apps are hosted with [IISNode](https://github.com/Azure/iisnode), and your Node.js app should listen to the port specified in the `process.env.PORT` variable. The following example shows how you do it in a simple Express app:
95+
In App Service on Windows, Node.js apps are hosted with [IISNode](https://github.com/Azure/iisnode), and your Node.js app should listen to the port specified in the `process.env.PORT` variable. The following example shows how to do that in a simple Express app:
9696

9797
::: zone-end
9898

9999
::: zone pivot="platform-linux"
100100

101-
App Service sets the environment variable `PORT` in the Node.js container, and forwards the incoming requests to your container at that port number. To receive the requests, your app should listen to that port using `process.env.PORT`. The following example shows how you do it in a simple Express app:
101+
App Service sets the environment variable `PORT` in the Node.js container, and forwards the incoming requests to your container at that port number. To receive the requests, your app should listen to that port using `process.env.PORT`. The following example shows how to do that in a simple Express app:
102102

103103
::: zone-end
104104

@@ -120,39 +120,39 @@ app.listen(port, () => {
120120

121121
## Customize build automation
122122

123-
If you deploy your app using Git, or zip packages [with build automation enabled](deploy-zip.md#enable-build-automation-for-zip-deploy), the App Service build automation steps through the following sequence:
123+
If you deploy your app by using Git, or by using zip packages [with build automation enabled](deploy-zip.md#enable-build-automation-for-zip-deploy), the App Service build automation steps through the following sequence:
124124

125-
1. Run custom script if specified by `PRE_BUILD_SCRIPT_PATH`.
125+
1. Run custom script, if one is specified by `PRE_BUILD_SCRIPT_PATH`.
126126
1. Run `npm install` without any flags, which includes npm `preinstall` and `postinstall` scripts and also installs `devDependencies`.
127127
1. Run `npm run build` if a build script is specified in your *package.json*.
128128
1. Run `npm run build:azure` if a build:azure script is specified in your *package.json*.
129129
1. Run custom script if specified by `POST_BUILD_SCRIPT_PATH`.
130130

131131
> [!NOTE]
132-
> As described in [npm docs](https://docs.npmjs.com/misc/scripts), scripts named `prebuild` and `postbuild` run before and after `build`, respectively, if specified. `preinstall` and `postinstall` run before and after `install`, respectively.
132+
> As is noted in the [npm docs](https://docs.npmjs.com/misc/scripts), scripts named `prebuild` and `postbuild` run before and after `build`, respectively, if specified. `preinstall` and `postinstall` run before and after `install`, respectively.
133133
134134
`PRE_BUILD_COMMAND` and `POST_BUILD_COMMAND` are environment variables that are empty by default. To run pre-build commands, define `PRE_BUILD_COMMAND`. To run post-build commands, define `POST_BUILD_COMMAND`.
135135

136-
The following example specifies the two variables to a series of commands, separated by commas.
136+
The following example uses the two variables to specify a series of commands, separated by commas.
137137

138138
```azurecli-interactive
139139
az webapp config appsettings set --name <app-name> --resource-group <resource-group-name> --settings PRE_BUILD_COMMAND="echo foo, scripts/prebuild.sh"
140140
az webapp config appsettings set --name <app-name> --resource-group <resource-group-name> --settings POST_BUILD_COMMAND="echo foo, scripts/postbuild.sh"
141141
```
142142

143-
For additional environment variables to customize build automation, see [Oryx configuration](https://github.com/microsoft/Oryx/blob/master/doc/configuration.md).
143+
For information about additional environment variables to customize build automation, see [Oryx configuration](https://github.com/microsoft/Oryx/blob/master/doc/configuration.md).
144144

145145
For more information on how App Service runs and builds Node.js apps in Linux, see [Oryx documentation: How Node.js apps are detected and built](https://github.com/microsoft/Oryx/blob/master/doc/runtimes/nodejs.md).
146146

147147
## Configure Node.js server
148148

149-
The Node.js containers come with [PM2](https://pm2.keymetrics.io/), a production process manager. You can configure your app to start with PM2, or with NPM, or with a custom command.
149+
The Node.js containers come with [PM2](https://pm2.keymetrics.io/), a production process manager. You can configure your app to start with PM2, with npm start, or with a custom command.
150150

151151
|Tool|Purpose|
152152
|--|--|
153153
|[Run with PM2](#run-with-pm2)|**Recommended** - Production or staging use. PM2 provides a full-service app management platform.|
154-
|[Run npm start](#run-npm-start)|Development use only.|
155-
|[Run custom command](#run-custom-command)|Either development or staging.|
154+
|[Run with npm start](#run-with-npm-start)|Development use only.|
155+
|[Run with a custom command](#run-with-a-custom-command)|Either development or staging.|
156156

157157
### Run with PM2
158158

@@ -163,7 +163,7 @@ The container automatically starts your app with PM2 when one of the common Node
163163
- *app.js*
164164
- *index.js*
165165
- *hostingstart.js*
166-
- One of the following [PM2 files](https://pm2.keymetrics.io/docs/usage/application-declaration/#process-file): *process.json* and *ecosystem.config.js*
166+
- One of the following [PM2 files](https://pm2.keymetrics.io/docs/usage/application-declaration/#process-file): *process.json* or *ecosystem.config.js*
167167

168168
You can also configure a custom start file with the following extensions:
169169

@@ -179,15 +179,15 @@ To add a custom start file, run the following command in the [Cloud Shell](https
179179
az webapp config set --resource-group <resource-group-name> --name <app-name> --startup-file "<filename-with-extension>"
180180
```
181181

182-
### Run custom command
182+
### Run with a custom command
183183

184184
App Service can start your app using a custom command, such as an executable like *run.sh*. For example, to run `npm run start:prod`, run the following command in the [Cloud Shell](https://shell.azure.com):
185185

186186
```azurecli-interactive
187187
az webapp config set --resource-group <resource-group-name> --name <app-name> --startup-file "npm run start:prod"
188188
```
189189

190-
### Run npm start
190+
### Run with npm start
191191

192192
To start your app using `npm start`, just make sure a `start` script is in the *package.json* file. For example:
193193

@@ -210,9 +210,6 @@ az webapp config set --resource-group <resource-group-name> --name <app-name> --
210210

211211
## Debug remotely
212212

213-
> [!NOTE]
214-
> Remote debugging is currently in Preview.
215-
216213
You can debug your Node.js app remotely in [Visual Studio Code](https://code.visualstudio.com/) if you configure it to [run with PM2](#run-with-pm2), except when you run it using a *.config.js,*.yml, or *.yaml*.
217214

218215
In most cases, no extra configuration is required for your app. If your app is run with a *process.json* file (default or custom), it must have a `script` property in the JSON root. For example:
@@ -227,9 +224,9 @@ In most cases, no extra configuration is required for your app. If your app is r
227224

228225
To set up Visual Studio Code for remote debugging, install the [App Service extension](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-azureappservice). Follow the instructions on the extension page and sign in to Azure in Visual Studio Code.
229226

230-
In the Azure explorer, find the app you want to debug, right-click it and select **Start Remote Debugging**. Click **Yes** to enable it for your app. App Service starts a tunnel proxy for you and attaches the debugger. You can then make requests to the app and see the debugger pausing at break points.
227+
In the Azure explorer, find the app you want to debug, right-click it and select **Start Remote Debugging**. Select **Yes** to enable remote debugging for your app. App Service starts a tunnel proxy for you and attaches the debugger. You can then make requests to the app and see the debugger pausing at break points.
231228

232-
Once finished with debugging, stop the debugger by selecting **Disconnect**. When prompted, you should click **Yes** to disable remote debugging. To disable it later, right-click your app again in the Azure explorer and select **Disable Remote Debugging**.
229+
Once finished with debugging, stop the debugger by selecting **Disconnect**. When prompted, you should select **Yes** to disable remote debugging. To disable it later, right-click your app again in the Azure explorer and select **Disable Remote Debugging**.
233230

234231
::: zone-end
235232

@@ -243,7 +240,7 @@ process.env.NODE_ENV
243240

244241
## Run Grunt/Bower/Gulp
245242

246-
By default, App Service build automation runs `npm install --production` when it recognizes a Node.js app is deployed through Git, or through Zip deployment [with build automation enabled](deploy-zip.md#enable-build-automation-for-zip-deploy). If your app requires any of the popular automation tools, such as Grunt, Bower, or Gulp, you need to supply a [custom deployment script](https://github.com/projectkudu/kudu/wiki/Custom-Deployment-Script) to run it.
243+
By default, App Service build automation runs `npm install --production` when it recognizes that a Node.js app is deployed through Git, or through Zip deployment [with build automation enabled](deploy-zip.md#enable-build-automation-for-zip-deploy). If your app requires any of the popular automation tools, such as Grunt, Bower, or Gulp, you need to supply a [custom deployment script](https://github.com/projectkudu/kudu/wiki/Custom-Deployment-Script) to run it.
247244

248245
To enable your repository to run these tools, you need to add them to the dependencies in *package.json.* For example:
249246

@@ -256,7 +253,7 @@ To enable your repository to run these tools, you need to add them to the depend
256253
}
257254
```
258255

259-
From a local terminal window, change directory to your repository root and run the following commands:
256+
From a local terminal window, change the directory to your repository root and run the following commands:
260257

261258
```bash
262259
npm install kuduscript -g
@@ -322,9 +319,9 @@ fi
322319

323320
## Detect HTTPS session
324321

325-
In App Service, [TLS/SSL termination](https://wikipedia.org/wiki/TLS_termination_proxy) happens at the network load balancers, so all HTTPS requests reach your app as unencrypted HTTP requests. If your app logic needs to check if the user requests are encrypted or not, inspect the `X-Forwarded-Proto` header.
322+
In App Service, [TLS/SSL termination](https://wikipedia.org/wiki/TLS_termination_proxy) happens at the network load balancers, so all HTTPS requests reach your app as unencrypted HTTP requests. If your app logic needs to check if the user requests are encrypted, inspect the `X-Forwarded-Proto` header.
326323

327-
Popular web frameworks let you access the `X-Forwarded-*` information in your standard app pattern. In [Express](https://expressjs.com/), you can use [trust proxies](https://expressjs.com/guide/behind-proxies.html). For example:
324+
Popular web frameworks let you access the `X-Forwarded-*` information in your standard app pattern. In [Express](https://expressjs.com/), you can use [trust proxies](https://expressjs.com/en/guide/behind-proxies.html). For example:
328325

329326
```javascript
330327
app.set('trust proxy', 1)
@@ -352,12 +349,12 @@ if (req.secure) {
352349

353350
## URL rewrites
354351

355-
When deploying Node.js apps on Azure App Service for Linux, you may need to handle URL rewrites directly within your application. This is particularly useful for ensuring specific URL patterns are redirected to the correct endpoints without relying on web server configurations. There are several ways to accomplish URL rewrites in Node.js. One example is through the [express-urlrewrite](https://www.npmjs.com/package/express-urlrewrite) package.
352+
When deploying Node.js apps on Azure App Service for Linux, you might need to handle URL rewrites directly within your application. This is particularly useful for ensuring specific URL patterns are redirected to the correct endpoints without relying on web server configurations. There are several ways to accomplish URL rewrites in Node.js. One example is through the [express-urlrewrite](https://www.npmjs.com/package/express-urlrewrite) package.
356353

357354

358355
## Monitor with Application Insights
359356

360-
Application Insights allows you to monitor your application's performance, exceptions, and usage without making any code changes. To attach the App Insights agent, go to your web app in the Portal and select **Application Insights** under **Settings**, then select **Turn on Application Insights**. Next, select an existing App Insights resource or create a new one. Finally, select **Apply** at the bottom. To instrument your web app using PowerShell, please see [these instructions](/azure/azure-monitor/app/azure-web-apps-nodejs#enable-through-powershell)
357+
Application Insights allows you to monitor your application's performance, exceptions, and usage without making any code changes. To attach the Application Insights agent, go to your web app in the portal, select **Application Insights** under **Settings**, and then select **Turn on Application Insights**. Next, select an existing Application Insights resource or create a new one. Finally, select **Apply** at the bottom. To instrument your web app using PowerShell, see [these instructions](/azure/azure-monitor/app/azure-web-apps-nodejs#enable-through-powershell)
361358

362359
This agent will monitor your server-side Node.js application. To monitor your client-side JavaScript, [add the JavaScript SDK to your project](/azure/azure-monitor/app/javascript).
363360

@@ -371,16 +368,16 @@ When a working Node.js app behaves differently in App Service or has errors, try
371368

372369
- [Access the log stream](#access-diagnostic-logs).
373370
- Test the app locally in production mode. App Service runs your Node.js apps in production mode, so you need to make sure that your project works as expected in production mode locally. For example:
374-
- Depending on your *package.json*, different packages may be installed for production mode (`dependencies` vs. `devDependencies`).
375-
- Certain web frameworks may deploy static files differently in production mode.
376-
- Certain web frameworks may use custom startup scripts when running in production mode.
377-
- Run your app in App Service in development mode. For example, in [MEAN.js](https://meanjs.org/), you can set your app to development mode in runtime by [setting the `NODE_ENV` app setting](configure-common.md).
371+
- Depending on your *package.json*, different packages might be installed for production mode (`dependencies` vs. `devDependencies`).
372+
- Certain web frameworks might deploy static files differently in production mode.
373+
- Certain web frameworks might use custom startup scripts when running in production mode.
374+
- Run your app in App Service in development mode. For example, in [MEAN.js](https://meanjs.org/), you can set your app to development mode at runtime by [setting the `NODE_ENV` app setting](configure-common.md).
378375

379376
::: zone pivot="platform-windows"
380377

381378
#### You do not have permission to view this directory or page
382379

383-
After deploying your Node.js code to a native Windows app in App Service, you may see the message `You do not have permission to view this directory or page.` in the browser when navigating to your app's URL. This is most likely because you don't have a *web.config* file (see the [template](https://github.com/projectkudu/kudu/blob/master/Kudu.Core/Scripts/iisnode.config.template) and an [example](https://github.com/Azure-Samples/nodejs-docs-hello-world/blob/master/web.config)).
380+
After deploying your Node.js code to a native Windows app in App Service, you might see the message `You do not have permission to view this directory or page` in the browser when navigating to your app's URL. This is most likely because you don't have a *web.config* file. (See the [template](https://github.com/projectkudu/kudu/blob/master/Kudu.Core/Scripts/iisnode.config.template) and an [example](https://github.com/Azure-Samples/nodejs-docs-hello-world/blob/master/web.config).)
384381

385382
If you deploy your files by using Git, or by using ZIP deployment [with build automation enabled](deploy-zip.md#enable-build-automation-for-zip-deploy), the deployment engine generates a *web.config* in the web root of your app (`%HOME%\site\wwwroot`) automatically if one of the following conditions is true:
386383

@@ -389,7 +386,7 @@ If you deploy your files by using Git, or by using ZIP deployment [with build au
389386

390387
The generated *web.config* is tailored to the detected start script. For other deployment methods, add this *web.config* manually. Make sure the file is formatted properly.
391388

392-
If you use [ZIP deployment](deploy-zip.md) (through Visual Studio Code, for example), be sure to [enable build automation](deploy-zip.md#enable-build-automation-for-zip-deploy) because it's not enabled by default. [`az webapp up`](/cli/azure/webapp#az-webapp-up) uses ZIP deployment with build automation enabled.
389+
If you use [ZIP deployment](deploy-zip.md) (through Visual Studio Code, for example), be sure to [enable build automation](deploy-zip.md#enable-build-automation-for-zip-deploy). It's not enabled by default. [`az webapp up`](/cli/azure/webapp#az-webapp-up) uses ZIP deployment with build automation enabled.
393390

394391
::: zone-end
395392

@@ -407,7 +404,7 @@ If you use [ZIP deployment](deploy-zip.md) (through Visual Studio Code, for exam
407404
::: zone pivot="platform-linux"
408405

409406
> [!div class="nextstepaction"]
410-
> [App Service Linux FAQ](faq-app-service-linux.yml)
407+
> [Azure App Service on Linux FAQ](faq-app-service-linux.yml)
411408
412409
::: zone-end
413410

0 commit comments

Comments
 (0)