Deploy node app to azure

Deploy node app to azure DEFAULT

1. Deploy to Azure App Service using the Azure CLI

  • 2 minutes to read

In this tutorial, you deploy a Node.js application to Azure App Service using the Azure Command Line Interface (CLI). With the Azure CLI you can:

  • Create Azure resource.

  • Set up a deployment pipeline between a Git repository and Azure.

  • Push changes to Azure App service

  • View the app's output in a streaming log.

  • Sample code - simple express application

Prerequisites

  • Use Azure Cloud Shell using the bash environment.

    Launch Cloud Shell in a new window

  • If you prefer, install the Azure CLI to run CLI reference commands.

    • If you're using a local install, sign in with Azure CLI by using the az login command. To finish the authentication process, follow the steps displayed in your terminal. See Sign in with Azure CLI for additional sign-in options.
    • When you're prompted, install Azure CLI extensions on first use. For more information about extensions, see Use extensions with Azure CLI.
    • Run az version to find the version and dependent libraries that are installed. To upgrade to the latest version, run az upgrade.

Sign in to Azure with Azure CLI

  1. Run the command.

    If the CLI can open your default browser, it will do so and load an Azure sign-in page.

    Otherwise, open a browser page at https://aka.ms/devicelogin and enter the authorization code displayed in your terminal.

  2. Sign in with your account credentials in the browser.

Next step

Feedback

View all page feedback

Sours: https://docs.microsoft.com/en-us/azure/developer/javascript/tutorial/tutorial-vscode-azure-cli-node/tutorial-vscode-azure-cli-node-01

Build and deploy a Node.js web application using Express on an Azure Cloud Services (classic)

Node.js includes a minimal set of functionality in the core runtime. Developers often use 3rd party modules to provide additional functionality when developing a Node.js application. In this tutorial you will create a new application using the Express module, which provides an MVC framework for creating Node.js web applications.

A screenshot of the completed application is below:

A web browser displaying Welcome to Express in Azure

Create a Cloud Service Project

Note

To successfully complete this section, you must have a working installation of Node.js and the Azure SDK for Node.js for your platform.

Perform the following steps to create a new cloud service project named 'expressapp':

  1. From the Start Menu or Start Screen, search for Windows PowerShell. Finally, right-click Windows PowerShell and select Run As Administrator.

    Azure PowerShell icon

  2. Change directories to the c:\node directory and then enter the following commands to create a new solution named expressapp and a web role named WebRole1:

    Note

    By default, Add-AzureNodeWebRole uses an older version of Node.js. The Set-AzureServiceProjectRole statement above instructs Azure to use v0.10.21 of Node. Note the parameters are case-sensitive. You can verify the correct version of Node.js has been selected by checking the engines property in WebRole1\package.json.

Install Express

  1. Install the Express generator by issuing the following command:

    The output of the npm command should look similar to the result below.

    Windows PowerShell displaying the output of the npm install express command.

  2. Change directories to the WebRole1 directory and use the express command to generate a new application:

    You will be prompted to overwrite your earlier application. Enter y or yes to continue. Express will generate the app.js file and a folder structure for building your application.

    The output of the express command

  3. To install additional dependencies defined in the package.json file, enter the following command:

    The output of the npm install command

  4. Use the following command to copy the bin/www file to server.js. This is so the cloud service can find the entry point for this application.

    After this command completes, you should have a server.js file in the WebRole1 directory.

  5. Modify the server.js to remove one of the '.' characters from the following line.

    After making this modification, the line should appear as follows.

    This change is required since we moved the file (formerly bin/www,) to the same directory as the app file being required. After making this change, save the server.js file.

  6. Use the following command to run the application in the Azure emulator:

    A web page containing welcome to express.

Modifying the View

Now modify the view to display the message "Welcome to Express in Azure".

  1. Enter the following command to open the index.jade file:

    The contents of the index.jade file.

    Jade is the default view engine used by Express applications. For more information on the Jade view engine, see http://jade-lang.com.

  2. Modify the last line of text by appending in Azure.

    The index.jade file, the last line reads: p Welcome to #{title} in Azure

  3. Save the file and exit Notepad.

  4. Refresh your browser and you will see your changes.

    A browser window, the page contains Welcome to Express in Azure

After testing the application, use the Stop-AzureEmulator cmdlet to stop the emulator.

Publishing the Application to Azure

In the Azure PowerShell window, use the Publish-AzureServiceProject cmdlet to deploy the application to a cloud service

Once the deployment operation completes, your browser will open and display the web page.

A web browser displaying the Express page. The URL indicates it is now hosted on Azure.

Next steps

For more information, see the Node.js Developer Center.

Sours: https://docs.microsoft.com/en-us/azure/cloud-services/cloud-services-nodejs-develop-deploy-express-app
  1. Colorado income tax refund status
  2. Microwave baked potato food network
  3. Oregon house of representatives members
  4. Rebuilt title cars near me

Thanks for the kind feedback, Raedbenz.

To begin with;

1- There's no db credentials included in the deployed app, as this Node.js app doesn't store data. Only uses Express (from the app.js code) for rendering the files. It gets data inputs from the home.ejs file, and directly sends the data to its respective prop (overrides the prop value) in the welcome.ejs file. Hence, no data is stored in the actual sense.

Moreso, Azure App Service renders web pages using the :3000 port number, by default. And as can be seen in the package.json file, there's a process.env.PORT||5000 argument - which is an environment configuration, where PORT is the env variable for the port number (was never hardcoded). The || operator in JavaScript means OR in Layman's terms; which makes the package.json file suitable for both local and remote deployments. The 5000 port number value would only always get accessed by the 127.0.0.1 address (your localhost), UNLESS you go ahead and change the value for PORT in your App Service settings. Which brings me to the next point.

It is important to note, that, environment variables can always be set from the Configuration section (Application Settings option) of your app service Settings. You would only need to replace the values that you wish to have hidden in your code with the variable names (that hold these specific values) in your configuration settings on App Service. I destroyed the created resource group already, I could have showed a walkthrough via screenshots.

Finally,

2- Kudu doesn't issue the npm start command from the package.json* file to run the app (and there's obviously no npm start command in the **package.json file). The Kudu service uses the line below to run the Node.js app. It basically runs your issued command (value) passed to the "scripts" key in your package.json file.

The Kudu service has also been built to automatically look for an app.js or server.js file in your project, set it as the entry file, and use this file to run your Node.js project - if it doesn't find the script



in your project. You can read more about this process here: blog.lifeishao.com/2017/03/24/cust...

A MERN/MEAN project would be built using the npm run build command, and it basically does nothing unles

`

is specified in your package.json file. See here: docs.npmjs.com/misc/scripts

So, npm run build is useful whenever packages require a build process. You can rebuild packages before deploying by simply running the command, locally - before pushing to production (yeah, the Kudu service, by default, deploys your app to production)

You can also find a MEAN stack sample here, which includes a deploy.sh bash file for deployments. github.com/Azure-Samples/meanjs

I'd also be happy to write a more comprehensive tutorial on this in the future, if need be. Cheers :)

Sours: https://dev.to/bashirk/the-painless-way-to-deploying-your-nodejs-app-on-azure-part-2-5151
Deploy A Node App to Azure In 2'ish Minutes

Create a Node.js web app in Azure

In this quickstart, you'll 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.

This quickstart configures an App Service app in the Free tier and incurs no cost for your Azure subscription.

Set up your initial environment

  • Have an Azure account with an active subscription. Create an account for free.
  • Install Node.js and npm. Run the command to verify that Node.js is installed.
  • Install Azure CLI, with which you run commands in any shell to provision and configure Azure resources.

Create your Node.js application

In this step, you create a starter Node.js application and make sure it runs on your computer.

  1. Create a simple Node.js application using the Express Generator, which is installed by default with Node.js and NPM.

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

  3. Start the development server.

  4. In a browser, navigate to . You should see something like this:

    Running Express Application

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 environment variable. In your generated Express app, this environment variable is already used in the startup script bin/www (search for ).

Sign in to Azure

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

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

  3. In the App Service explorer, select Sign in to Azure... and follow the instructions.

    In Visual Studio Code, you should see your Azure email address in the Status Bar and your subscription in the AZURE APP SERVICE explorer.

    sign in to Azure

Configure the App Service app and deploy code

  1. In the App Service explorer, select the Deploy to Web App icon.

    Screenshot of the Azure App service in Visual Studio Code showing the blue arrow icon selected.

  2. Choose the myExpressApp folder.

  1. Choose Create new Web App. A Linux container is used by default.

  2. Type a globally unique name for your web app and press 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. An LTS version is recommended.

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

  5. In the popup Always deploy the workspace "myExpressApp" to <app-name>", select Yes. This way, 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 provisions the Azure resources and deploys the code, it shows progress notifications.

  6. Once deployment completes, select Browse Website in the notification popup. The browser should display the Express default page.

  1. Choose Create new Web App... Advanced.

  2. Type a globally unique name for your web app and press 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. Select Create a new resource group, then enter a name for the resource group, such as AppServiceQS-rg.

  4. Select the Node.js version you want. An LTS version is recommended.

  5. Select Windows for the operating system.

  6. Select the location you want to serve your app from. For example, West Europe.

  7. Select Create new App Service plan, then enter a name for the plan (such as AppServiceQS-plan), then select F1 Free for the pricing tier.

  8. For Select an Application Insights resource for your app, select Skip for now and wait the resources to be provisioned in Azure.

  9. In the popup Always deploy the workspace "myExpressApp" to <app-name>", select Yes. This way, 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 provisions the Azure resources and deploys the code, it shows progress notifications.

  10. In the App Service explorer in Visual Studio code, expand the node for the new app, right-click Application Settings, and select Add New Setting:

    Add app setting command

  11. Enter for the setting key.

  12. Enter for the setting value.

    This app setting enables build automation at deploy time, which automatically detects the start script and generates the web.config with it.

  13. In the App Service explorer, select the Deploy to Web App icon again, confirm by clicking Deploy again.

  14. Wait for deployment to complete, then select Browse Website in the notification popup. The browser should display the Express default page.

In the terminal, make sure you're in the myExpressApp directory, and deploy the code in your local folder (myExpressApp) using the command:

  • If the command isn't recognized, be sure you have the Azure CLI installed as described in Set up your initial environment.
  • Replace with a name that's unique across all of Azure (valid characters are , , and ). A good pattern is to use a combination of your company name and an app identifier.
  • The argument creates the web app on the Free pricing tier, which incurs a no cost.
  • You can optionally include the argument where is an available Azure region. You can retrieve a list of allowable regions for your Azure account by running the command.
  • The command creates a Linux app for Node.js by default. To create a Windows app instead, use the argument.
  • If you see the error, "Could not auto-detect the runtime stack of your app," make sure you're running the command in the myExpressApp directory (See Troubleshooting auto-detect issues with az webapp up).

The command may take a few minutes to complete. While running, it provides messages about creating the resource group, the App Service plan, and the app resource, configuring logging, and doing ZIP deployment. It then gives the message, "You can launch the app at http://<app-name>.azurewebsites.net", which is the app's URL on Azure.

The webapp '<app-name>' doesn't exist Creating Resource group '<group-name>' ... Resource group creation complete Creating AppServicePlan '<app-service-plan-name>' ... Creating webapp '<app-name>' ... Configuring default logging for the app, if not already enabled Creating zip with contents of dir /home/cephas/myExpressApp ... Getting scm site credentials for zip deployment Starting zip deployment. This operation can take a while to complete ... Deployment endpoint responded with status code 202 You can launch the app at http://<app-name>.azurewebsites.net { "URL": "http://<app-name>.azurewebsites.net", "appserviceplan": "<app-service-plan-name>", "location": "centralus", "name": "<app-name>", "os": "<os-type>", "resourcegroup": "<group-name>", "runtime_version": "node|10.14", "runtime_version_detected": "0.0", "sku": "FREE", "src_path": "//home//cephas//myExpressApp" }

Note

The command does the following actions:

  • Create a default resource group.

  • Create a default App Service plan.

  • Create an app with the specified name.

  • Zip deploy all files from the current working directory, with build automation enabled.

  • Cache the parameters locally in the .azure/config file so that you don't need to specify them again when deploying later with or other Azure CLI commands. The cached values are used automatically by default.

Redeploy updates

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

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

    to

  1. In the App Service explorer, select the Deploy to Web App icon again, confirm by clicking Deploy again.

  2. Wait for deployment to complete, then select Browse Website in the notification popup. You should see that the message has been changed to .

  1. Save your changes, then redeploy the app using the command again with no arguments:

    This command uses values that are cached locally in the .azure/config file, such as the app name, resource group, and App Service plan.

  2. Once deployment is complete, refresh the webpage . You should see that the message has been changed to .

Stream Logs

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

  1. In the App Service explorer, right-click the app node and choose Start Streaming Logs.

    Start Streaming Logs

  2. If asked to restart the app, click 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 .

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

The command uses the resource group name cached in the .azure/config file.

You can also include the parameter with then command to automatically open the log stream on deployment.

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, press Ctrl+C in the terminal.

Clean up resources

In the preceding steps, you created Azure resources in a resource group. The create 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, expand the Resource Groups explorer.

  2. Expand the subscription, right-click the resource group you created earlier, and select Delete.

    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. Once you confirm, the resource group is deleted, and you see a notification when it's done.

In the preceding steps, you created Azure resources in a resource group. The resource group has a name like "appsvc_rg_Linux_CentralUS" depending on your location.

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

The command uses the resource group name cached in the .azure/config file.

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

Next steps

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.

Feedback

View all page feedback

Sours: https://docs.microsoft.com/en-us/azure/app-service/quickstart-nodejs

To deploy azure app node

How to deploy your super cool Node app to Azure from GitHub

by Rohit Ramname

Are you a Nodejs developer who loves developing wonderful apps as a hobby but needs some place to show it off?

Are you a person fascinated by Azure and tempted to try it out?

Are you also that person overwhelmed by the configurations that Azure offers?

Well.. today I will be explaining a very simple way to get your app up and running on Azure in a few clicks.

I had published an article about deploying a “cool node app“ to Heroku from Github for free. But now that we have decided to build a “super cool” Node app, we will use another hot cloud service in the market.

Enough of chitchat…now let’s get to the business.

Step 1: Create that super cool Node app

Let’s create that super cool Node app first.

Create a folder on your local machine and give it a name (of your choice), say MySuperCoolApp.

Add a file with the name package.json and paste the below content. This file is basic information of our package. (This can also be created by typing command npm init and accepting all default settings.)

One very important change to notice is this line:

After the deployment, Azure will run this command to start your application.

Add a file, app.js, and paste the below code. This will be the starting point of our app.

This code is opening a port on the local server and serving some HTML.

Please note the most important code block here:

This is important when you want to deploy your application to the cloud. The application server is started on a random port on the cloud. If you hard code a port number, like in all Getting Started guides, and deploy to the cloud, the specific port number may not be available. The application will never start. So it’s better to get the port number assigned by the cloud instance and start the HTTP server.

Save the file and run the below command in the command prompt window (which is open inside the folder):

With this, Node will start the server and show the below message:

Now, if we open http://localhost:3000/ in the browser, we will see this:

Cool! We created a basic but super cool Nodejs app.

STEP 2: Push to GitHub

Now want to upload our code to GitHub. This way, we will be able to edit our code from anywhere and also deploy the committed changes to the cloud instantly.

Let’s create a Repository on GitHub by clicking New Repository.

Give it a name, some description, and click Create repository:

GitHub will create a repository and give you some commands that you can run locally so that you can clone your local folder with your GitHub repository.

Open command prompt inside your app where the package.json file is located. In the command prompt, run the below commands in this sequence.

  1. Initialize the Git repository at the root level:

2. Add all the files to your local Git (staging). Notice the last dot:

3. Commit your changes to your local Git:

4. Link to your GitHub repository. (Please change the URL to point to your repository.)

5. And push your change:

You should see messages like below at the command prompt.

Now if you open GitHub and refresh the repository, you should be able to see the code.

STEP 3: Now, let’s get it up on Azure

This exercise assumes that you have a Microsoft Azure subscription set up and ready for use. If you don’t have one, you can create it for free by going to Microsoft’s Azure website. It will ask you for your credit card information. Your card is never charged unless you purchase paid services (which is not required for this demo).

Open portal.azure.com and login with your credentials.

Click Create New Resource on the top left. Enter “web app” in the search box to get the required resource type and hit Enter.

Select Web App from the search results.

And click Create button.

We will be asked to provide some basic information for this App:

The first box is the name of your app. Which is super easy since its SuperCoolNodeApp :)

The second option is the subscription. I have registered for the “Pay As You Go” subscription since I have already consumed my free trial. You can select your Free Subscription plan here.

Next is the resource group. This is the logical grouping of your apps on Azure. You can create new for this app or use existing ones. I created a new one for this app as SuperCoolNodeApp.

Then, in the end, you will have to select the app service plan. I have created a free plan with the name Test Plan. You can create a new plan if you don’t already have one but make sure you select a Free Version. While selecting, Azure automatically selects S1 tier which is NOT FREE. Make sure you change it to free plan for the demo (Of course, if you want higher capabilities, processing powers etc, go for paid plans.)

Click Create.

Azure will queue up your request for creating an App with the service plan that you chose and show you a small notification at the top. Creating an app should not take much longer. If you refresh your page in a minute or 2, you should be able to see the app and service plan that was created under all Resources.

Now, click on it to see the details of the app that we just created.

It shows the detail like the subscription the plan is running on, status as Running, Subscription ID, Location on the server that the app is hosted on “Central US” and some FTP details. But the most important thing here is the URL. That is going to be the URL of our application in the cloud.

Now let’s get it there….

A little spoiler alert, :) In this section, we will configuring the deployment strategy for our application.

Scroll down and click on Deployment Options.

Click on Configure required settings and select GitHub. It should show you the below screen.

Click Choose project. This should show you all the repositories on your GitHub account.

If you are doing this for the first time, you will have to provide Azure the authorization to access your GitHub account.

Here you will be selecting that MySuperCoolNodeApp that you pushed to GitHub.

Next, we can select the branch that we want to deploy from.

For now, I only have master so I am leaving the default one.

And that’s it. Click OK.

Azure will take care of deploying the app. It will even show you this little notification that Azure is on his way to do this job.

When it’s done (which really shouldn’t take that long), click on the Deployment options again. You should be able to see the last deployment.

If you click on the record, Azure will even show the log of the deployment event.

Cool. Now, if you open your app by going to the URL mentioned in the overview section https://supercoolnodeapp.azurewebsites.net/, you expect to see the Hello World message but instead you see the error below.

Huh… what is wrong? Logs show that the application was deployed, you don’t see any errors, but the app does not work. It’s a mystery.

There is one small setting that you need to do on the Azure portal to help Azure treat it as a Nodejs app and start it accordingly.

Open the Application Settings and scroll down to the Application Settings section and add the below entry.

It’s basically telling Azure to use this Node version and open the website.

Click Save at the top.

Now if you go to the URL https://supercoolnodeapp.azurewebsites.net/

WOOHOO!!! There you go. We just got our super cool Node app up and running in Azure.

Congratulations!! Now every time you make any change to your app and push to GitHub, Azure will catch those and do the continuous deployment.

P.S : If you ever build an app cooler than mine :), then please do share.

If this article helped you , I love applause here and connects on twitter :)

I only write about programming and technology on Twitter.

Have fun!!


If this article was helpful, tweet it.

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

Sours: https://www.freecodecamp.org/news/how-to-deploy-your-super-cool-node-app-to-azure-from-github-47ebff6c5448/
Build and deploy Node.js and React apps with Visual Studio Code, Azure App Service and Cosmos DB

Configure a Node.js app for Azure App Service

Node.js apps must be deployed with all the required NPM dependencies. The App Service deployment engine automatically runs for you when you deploy a Git repository, or a Zip packagewith build automation enabled. If you deploy your files using FTP/S, however, you need to upload the required packages manually.

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 and Node.js with MongoDB tutorial first.

Show Node.js version

To show the current Node.js version, run the following command in the Cloud Shell:

To show all supported Node.js versions, navigate to or run the following command in the Cloud Shell:

To show the current Node.js version, run the following command in the Cloud Shell:

To show all supported Node.js versions, run the following command in the Cloud Shell:

Set Node.js version

To set your app to a supported Node.js version, run the following command in the Cloud Shell to set to a supported version:

This setting specifies the Node.js version to use, both at runtime and during automated package restore during App Service build automation. This setting only recognizes major minor versions, the LTS moniker is not supported.

Note

You should set the Node.js version in your project's . The deployment engine runs in a separate process that contains all the supported Node.js versions.

To set your app to a supported Node.js version, run the following command in the Cloud Shell:

This setting specifies the Node.js version to use, both at runtime and during automated package restore in Kudu.

Note

You should set the Node.js version in your project's . The deployment engine runs in a separate container that contains all the supported Node.js versions.

Get port number

You Node.js app needs to listen to the right port to receive incoming requests.

In App Service on Windows, Node.js apps are hosted with IISNode, and your Node.js app should listen to the port specified in the variable. The following example shows how you do it in a simple Express app:

App Service sets the environment variable 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 . The following example shows how you do it in a simple Express app:

Customize build automation

If you deploy your app using Git, or zip packages with build automation enabled, the App Service build automation steps through the following sequence:

  1. Run custom script if specified by .
  2. Run without any flags, which includes npm and scripts and also installs .
  3. Run if a build script is specified in your package.json.
  4. Run if a build:azure script is specified in your package.json.
  5. Run custom script if specified by .

Note

As described in npm docs, scripts named and run before and after , respectively, if specified. and run before and after , respectively.

and are environment variables that are empty by default. To run pre-build commands, define . To run post-build commands, define .

The following example specifies the two variables to a series of commands, separated by commas.

For additional environment variables to customize build automation, see Oryx configuration.

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.

Configure Node.js server

The Node.js containers come with PM2, a production process manager. You can configure your app to start with PM2, or with NPM, or with a custom command.

Run with PM2

The container automatically starts your app with PM2 when one of the common Node.js files is found in your project:

  • bin/www
  • server.js
  • app.js
  • index.js
  • hostingstart.js
  • One of the following PM2 files: process.json and ecosystem.config.js

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

  • A .js file
  • A PM2 file with the extension .json, .config.js, .yaml, or .yml

To add a custom start file, run the following command in the Cloud Shell:

Run custom command

App Service can start your app using a custom command, such as an executable like run.sh. For example, to run , run the following command in the Cloud Shell:

Run npm start

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

To use a custom package.json in your project, run the following command in the Cloud Shell:

Debug remotely

Note

Remote debugging is currently in Preview.

You can debug your Node.js app remotely in Visual Studio Code if you configure it to run with PM2, except when you run it using a *.config.js, *.yml, or .yaml.

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 property in the JSON root. For example:

To set up Visual Studio Code for remote debugging, install the App Service extension. Follow the instructions on the extension page and sign in to Azure in Visual Studio Code.

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.

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.

Access environment variables

In App Service, you can set app settings outside of your app code. Then you can access them using the standard Node.js pattern. For example, to access an app setting called , use the following code:

Run Grunt/Bower/Gulp

By default, App Service build automation runs when it recognizes a Node.js app is deployed through Git, or through Zip deployment with build automation enabled. If your app requires any of the popular automation tools, such as Grunt, Bower, or Gulp, you need to supply a custom deployment script to run it.

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

From a local terminal window, change directory to your repository root and run the following commands:

Your repository root now has two additional files: .deployment and deploy.sh.

Open deploy.sh and find the section, which looks like this:

This section ends with running . Add the code section you need to run the required tool at the end of the section:

See an example in the MEAN.js sample, where the deployment script also runs a custom command.

Bower

This snippet runs .

Gulp

This snippet runs .

Grunt

This snippet runs .

Detect HTTPS session

In App Service, TLS/SSL termination 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 header.

Popular web frameworks let you access the information in your standard app pattern. In Express, you can use trust proxies. For example:

Access diagnostic logs

To access the console logs generated from inside your application code in App Service, turn on diagnostics logging by running the following command in the Cloud Shell:

Possible values for are: , , , and . Each subsequent level includes the previous level. For example: includes only error messages, and includes all messages.

Once diagnostic logging is turned on, run the following command to see the log stream:

If you don't see console logs immediately, check again in 30 seconds.

Note

You can also inspect the log files from the browser at .

To stop log streaming at any time, type +.

You can access the console logs generated from inside the container.

First, turn on container logging by running the following command:

Replace and with the names appropriate for your web app.

Once container logging is turned on, run the following command to see the log stream:

If you don't see console logs immediately, check again in 30 seconds.

To stop log streaming at any time, type Ctrl+C.

You can also inspect the log files in a browser at .

Monitor with Application Insights

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

This agent will monitor your server-side Node.js application. To monitor your client-side JavaScript, add the JavaScript SDK to your project.

For more information, see the Application Insights extension release notes.

Troubleshooting

When a working Node.js app behaves differently in App Service or has errors, try the following:

  • Access the log stream.
  • 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:
    • Depending on your package.json, different packages may be installed for production mode ( vs. ).
    • Certain web frameworks may deploy static files differently in production mode.
    • Certain web frameworks may use custom startup scripts when running in production mode.
  • Run your app in App Service in development mode. For example, in MEAN.js, you can set your app to development mode in runtime by setting the app setting.

You do not have permission to view this directory or page

After deploying your Node.js code to a native Windows app in App Service, you may see the message 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 and an example).

If you deploy your files by using Git, or by using ZIP deployment with build automation enabled, the deployment engine generates a web.config in the web root of your app () automatically if one of the following conditions is true:

  • Your project root has a package.json that defines a script that contains the path of a JavaScript file.
  • Your project root has either a server.js or an app.js.

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.

If you use ZIP deployment (through Visual Studio Code, for example), be sure to enable build automation because it's not enabled by default. uses ZIP deployment with build automation enabled.

robots933456 in logs

You may see the following message in the container logs:

You can safely ignore this message. is a dummy URL path that App Service uses to check if the container is capable of serving requests. A 404 response simply indicates that the path doesn't exist, but it lets App Service know that the container is healthy and ready to respond to requests.

Next steps

Or, see additional resources:

Environment variables and app settings reference

Sours: https://docs.microsoft.com/en-us/azure/app-service/configure-language-nodejs

You will also be interested:

Deploy Express.js to Azure App Service using Visual Studio Code

  • 4 minutes to read

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

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.

  1. In Visual Studio Code, select the Azure explorer icon, then select Sign in to Azure, and follow the prompts.

    Sign in to Azure through VS Code

  2. 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:

    VS Code Azure explorer showing subscriptions

3. Create a local Express.js app

Create and run an Express.js app by cloning an Azure sample repository.

  1. At a terminal command prompt, go to the location where you want to create the app folder.

  2. Use the following base command with git to clone the repository, change into the repository folder named , then install the npm dependencies, and open the project in Visual Studio Code from the terminal.

4. Run your local Express.js app

  1. Start the server:

  2. Test the app by opening a browser to . The site should appear as follows:

    Running Express Application

  3. Press Ctrl+C in the terminal to stop the server.

5. Create App service resource in Visual Studio Code

  1. Use Ctrl+Shift+P to open the command palette.

  2. Enter then 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.

  3. 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 , and select the F1 Freepricing tier.
    • Select Skip for now for the Application Insights resource.
    • Select a location near you.
  4. After a short time, VS Code notifies you that creation is complete. Close the notification with the X button:

    Notification on completion of web app creation

  5. 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.

    Configure deployment source command on a web app

  6. When prompted, select LocalGit.

  7. Select the deployment (up arrow) icon to deploy your code to Azure:

    Deploy to Web App icon

  8. At the prompts, select the myexpressapp folder, select your subscription account again and then select the name of the web app created earlier.

    When this deployment is complete, it is listed in the Azure Explorer, under your web app's Deployments Git node.

  9. When prompted with Always deploy the workspace to (app name)", select Yes. This tells VS Code to automatically target the same App Service web app with subsequent deployments.

  10. 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 . This output appears in the Output window in Visual Studio Code.

  1. In the Azure App Service explorer, right-click the app node and choose Start Streaming Logs.

    View Streaming Logs

  2. Refresh the web page a few times in the browser to see additional log output.

8. Make changes and redeploy

  1. Make a small change to the app. In , change to .

  2. Right-click your app service from the list of App services in the App service extension, then select Deploy to Web App....

    Deploy or redeploy to App service with Visual Studio Code

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.

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**.

Troubleshooting

When trying to sign in to Azure, 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 and environment variables with your proxy information in your terminal:

Next steps

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:

Feedback

View all page feedback

Sours: https://docs.microsoft.com/en-us/azure/developer/javascript/tutorial/deploy-nodejs-azure-app-service-with-visual-studio-code


2218 2219 2220 2221 2222