page_type | languages | products | description | urlFragment | ||
---|---|---|---|---|---|---|
sample |
|
|
This sample demonstrates how to manage your Azure websites using a node.js client. |
app-service-web-nodejs-manage |
This sample demonstrates how to manage your Azure websites using a node.js client.
On this page
-
If you don't already have it, get node.js.
-
Clone the repository.
git clone https://github.com:Azure-Samples/app-service-web-nodejs-manage.git
-
Install the dependencies.
cd app-service-web-nodejs-manage npm install
-
Create an Azure service principal either through Azure CLI, PowerShell or the portal.
-
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 ofexport
. -
Run the sample.
node index.js
-
To clean up after index.js, run the cleanup script.
node cleanup.js <resourceGroupName> <websiteName>
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);
}
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);
var parameters = {
location: location,
serverFarmId: expectedServerFarmId
};
webSiteClient.sites.createOrUpdateSite(resourceGroupName, webSiteName, parameters, callback);
webSiteClient.sites.getSites(resourceGroupName, callback);
webSiteClient.sites.getSite(resourceGroupName, webSiteName, callback);
var siteConfig = {
location: location,
serverFarmId: expectedServerFarmId,
numberOfWorkers: 2,
phpVersion: '5.5'
};
webSiteClient.sites.createOrUpdateSiteConfig(resourceGroupName, webSiteName, siteConfig, callback);
Please refer to Azure SDK for Node for more information.