You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Our project is now initialized with a *packages.json* file. Our project is going to use some Azure libraries contained in NPM packages. Let's add those to the project as dependencies.
41
+
Our project is now initialized with a *packages.json* file. Our project is going to use some Azure libraries contained in NPM packages. We'll be using the Azure Client Runtime for Node.js (ms-rest-azure) and the Azure CDN Client Library for Node.js (azure-arm-cd). Let's add those to the project as dependencies.
42
42
```
43
43
npm install --save ms-rest-azure
44
44
npm install --save azure-arm-cdn
45
45
```
46
46
47
-
Assuming the packages installed with no errors, if we view the *package.json* file, it should look similar to this (version numbers may differ):
47
+
After the packages are done installing, the *package.json* file should look similar to this (version numbers may differ):
48
48
49
49
```
50
50
{
@@ -66,26 +66,300 @@ Assuming the packages installed with no errors, if we view the *package.json* fi
66
66
67
67
Finally, using your text editor, create a blank text file and save it in the root of our project folder as *app.js*. We're now ready to begin writing code.
68
68
69
-
##
69
+
## Requires, constants, authentication, and structure
70
+
71
+
Let's get the basic structure of our program written.
72
+
73
+
1. Add the "requires" for our NPM packages at the top with the following:
74
+
75
+
```
76
+
var msRestAzure = require('ms-rest-azure');
77
+
var cdnManagementClient = require('azure-arm-cdn');
78
+
```
79
+
80
+
2. We need to define some constants our methods will use. Add the following. Be sure to replace the placeholders, including the **<angle brackets>**, with your own values as needed.
81
+
82
+
```
83
+
//Tenant app constants
84
+
const clientId = "<YOUR CLIENT ID>";
85
+
const clientSecret = "<YOUR CLIENT AUTHENTICATION KEY>"; //Only for service principals
86
+
const tenantId = "<YOUR TENANT ID>";
87
+
88
+
//Application constants
89
+
const subscriptionId = "<YOUR SUBSCRIPTION ID>";
90
+
const resourceGroupName = "CdnConsoleTutorial";
91
+
const resourceLocation = "<YOUR PREFERRED AZURE LOCATION, SUCH AS Central US>";
92
+
```
93
+
94
+
3. Next, we'll instantiate the CDN management client and give it our credentials.
95
+
96
+
```
97
+
var credentials = new msRestAzure.ApplicationTokenCredentials(clientId, tenantId, clientSecret);
98
+
var cdnClient = new cdnManagementClient(credentials, subscriptionId);
99
+
```
100
+
101
+
If you are using individual user authentication, these two lines will look slightly different.
102
+
103
+
>[AZURE.IMPORTANT] Only use this code sample if you are choosing to have individual user authentication instead of a service principal.
104
+
105
+
```
106
+
var credentials = new msRestAzure.UserTokenCredentials(clientId,
var cdnClient = new cdnManagementClient(credentials, subscriptionId);
109
+
```
110
+
111
+
Be sure to replace the items in **<angle brackets>** with the correct information. For `<redirect URI>`,use the redirect URI you entered when you registered the application in Azure AD.
112
+
113
+
114
+
4. Our Node.js console application is going to take some command line parameters. Let's validate that at least one parameter was passed.
115
+
116
+
```
117
+
//Collect command line parameters
118
+
var parms = process.argv.slice(2);
119
+
120
+
//Do we have parameters?
121
+
if(parms == null || parms.length == 0)
122
+
{
123
+
console.log("Not enough parameters!");
124
+
console.log("Valid commands are list, delete, create, and purge.");
125
+
process.exit(1);
126
+
}
127
+
```
128
+
129
+
5. That brings us to the main part of our program, where we'll branch off to other functions based on what parameters were passed.
130
+
131
+
```
132
+
switch(parms[0].toLowerCase())
133
+
{
134
+
case "list":
135
+
cdnList();
136
+
break;
137
+
138
+
case "create":
139
+
cdnCreate();
140
+
break;
141
+
142
+
case "delete":
143
+
cdnDelete();
144
+
break;
145
+
146
+
case "purge":
147
+
cdnPurge();
148
+
break;
149
+
150
+
default:
151
+
console.log("Valid commands are list, delete, create, and purge.");
152
+
process.exit(1);
153
+
}
154
+
```
155
+
156
+
6. At several places in our program, we'll need to make sure the right number of parameters were passed in and display some help if they don't look correct. Let's create functions to do that.
7. Finally, the functions we'll be using on CDN management client are asynchronous, so they need a method to callback when they're done. Let's make one that can display the output from the CDN management client (if any) and exit the program gracefully.
196
+
197
+
```
198
+
function callback(err, result, request, response) {
199
+
if (err) {
200
+
console.log(err);
201
+
process.exit(1);
202
+
} else {
203
+
console.log((result == null) ? "Done!" : result);
204
+
process.exit(0);
205
+
}
206
+
}
207
+
```
208
+
209
+
Now that the basic structure of our program is written, we should create the functions called based on our parameters.
210
+
211
+
## List CDN profiles and endpoints
212
+
213
+
Let's start with code to list our existing profiles and endpoints. I'll provide code comments with the expected syntax so we know which parameter goes where.
We can now compile and run the program by clicking the **Start** button in Visual Studio.
339
+
We can now execute our Node.js program using our favorite debugger or at the console.
340
+
341
+
> [AZURE.TIP] If you're using Visual Studio Code as your debugger, you'll need to setup your environment to pass in the command line parameters. Visual Studio Code does this in the **lanuch.json** file. Look for a property named **args** and add an array of string values for your parameters, so that it looks similar to this: `"args": ["list", "profiles"]`.
When the program reaches the above prompt, you should be able to return to your resource group in the Azure Portal and see that the profile has been created.
To see the completed project from this walkthrough, [download the sample](https://code.msdn.microsoft.com/Azure-CDN-Management-1f2fba2c).
361
+
To see the completed project from this walkthrough, [download the sample](https://code.msdn.microsoft.com/Azure-CDN-SDK-for-Nodejs-c712bc74).
88
362
89
-
To find additional documentation on the Azure CDN Management Library for .NET, view the [reference on MSDN](https://msdn.microsoft.com/library/mt657769.aspx).
363
+
To find additional documentation on the Azure SDK for Node.js, view the [documentation](http://azure.github.io/azure-sdk-for-node/).
0 commit comments