Manage Azure websites with Node.js

This sample demonstrates how to manage your Azure websites using a node.js client.

On this page

Run this sample

  1. If you don't already have it, get node.js.

  2. Clone the repository.

    git clone https://github.com:Azure-Samples/app-service-web-nodejs-manage.git
    
  3. Install the dependencies.

    cd app-service-web-nodejs-manage
    npm install
    
  4. Create an Azure service principal either through Azure CLI, PowerShell or the portal.

  5. Set the following environment variables using the information from the service principle that you created.

    export AZURE_SUBSCRIPION_ID={your subscription id}
    export CLIENT_ID={your client id}
    export APPLICATION_SECRET={your client secret}
    export DOMAIN={your tenant id as a guid OR the domain name of your org <contosocorp.com>}
    

    [AZURE.NOTE] On Windows, use set instead of export.

  6. Run the sample.

    node index.js
    
  7. To clean up after index.js, run the cleanup script.

    node cleanup.js <resourceGroupName> <websiteName>
    

What does index.js do?

The sample creates, lists and updates a website. It starts by logging in using your service principal.

_validateEnvironmentVariables();
var clientId = process.env['CLIENT_ID'];
var domain = process.env['DOMAIN'];
var secret = process.env['APPLICATION_SECRET'];
var subscriptionId = process.env['AZURE_SUBSCRIPTION_ID'];
var resourceClient, webSiteClient;
//Sample Config
var randomIds = {};
var location = 'westus';
var resourceGroupName = _generateRandomId('testrg', randomIds);
var hostingPlanName = _generateRandomId('plan', randomIds);
var webSiteName = _generateRandomId('testweb', randomIds);
var expectedServerFarmId;

///////////////////////////////////////
//Entrypoint for the sample script   //
///////////////////////////////////////

msRestAzure.loginWithServicePrincipalSecret(clientId, secret, domain, function (err, credentials) {
  if (err) return console.log(err);
  resourceClient = new ResourceManagementClient(credentials, subscriptionId);
  webSiteClient = new WebSiteManagement(credentials, subscriptionId);

The sample then sets up a resource group in which it will create the website.

function createResourceGroup(callback) {
  var groupParameters = { location: location, tags: { sampletag: 'sampleValue' } };
  console.log('\nCreating resource group: ' + resourceGroupName);
  return resourceClient.resourceGroups.createOrUpdate(resourceGroupName, groupParameters, callback);
}

Create a hosting plan

To create a website, you need a hosting plan. Here's how to create one.

var planParameters = {
  serverFarmWithRichSkuName: hostingPlanName,
  location: location,
  sku: {
    name: 'S1',
    capacity: 1,
    tier: 'Standard'
  }  
};
webSiteClient.serverFarms.createOrUpdateServerFarm(resourceGroupName, hostingPlanName, planParameters, callback);

Create a website

var parameters = {
  location: location,
  serverFarmId: expectedServerFarmId
};
webSiteClient.sites.createOrUpdateSite(resourceGroupName, webSiteName, parameters, callback);

List websites in the resourcegroup

webSiteClient.sites.getSites(resourceGroupName, callback);

Get details for the given website

webSiteClient.sites.getSite(resourceGroupName, webSiteName, callback);

Update site config(number of workers and phpversion) for the website

var siteConfig = {
  location: location,
  serverFarmId: expectedServerFarmId,
  numberOfWorkers: 2,
  phpVersion: '5.5'
};
webSiteClient.sites.createOrUpdateSiteConfig(resourceGroupName, webSiteName, siteConfig, callback);

More information

Please refer to Azure SDK for Node for more information.