Code Monkey home page Code Monkey logo

app-service-web-nodejs-manage's Introduction

page_type languages products description urlFragment
sample
javascript
azure
This sample demonstrates how to manage your Azure websites using a node.js client.
app-service-web-nodejs-manage

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.

app-service-web-nodejs-manage's People

Contributors

acomsmpbot avatar allclark avatar amarzavery avatar ezyakaeagle442 avatar v-hearya avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.