Node and Azure a quick start guide

This guide will show you how to create a basic node application and use git to deploy it to Windows Azure.

Prerequisites

Install git – This is used to deploy our code to Azure.
http://git-scm.com/
Select run git from the windows command prompt

Install nodejs
http://nodejs.org/

Install azure tools
npm install azure-cli --g

You could install the azure tools for PowerShell instead, but I prefer to use the npm (Node package manager) version of the tools, since we are using node. The commands given when using NPM will be the same regardless if you are using GNU Linux, OS X or Windows.

Configure Azure tools

We need to configure azure tools using our publisher settings from Windows Azure. Luckily this is very easy to setup.
First download the account information. The following command will open the browser and download a subscription file.
azure account download




Import the settings we just downloaded. Remember to delete the file afterwards as it contains sensitive information.

azure account import “filename and path from file downloaded in previous step”

This should be enough to finish configuring the azure tools. However if you have multiple subscriptions connected to your Live Id please read on.

You can use this command to view a list of subscriptions.
azure account list
If you have multiple subscriptions, you can use the following command to set the active subscription
azure account set ""

Create the node application

Create a node application, which we can deploy. Create a new folder where we will place the files needed for the node application.
First create a package.json file using this content.
{
    "name": "hello-world",
    "description": "hello world test app",
    "version": "0.0.1",
    "private": true,
    "dependencies": {
        "express": "3.x"
    }
}

Then create a app.js file with this content.

var express = require('express');
var app = express();

app.get('/', function (req, res) {
    res.send('Hello World');
});

var port = process.env.PORT || 5000;

var server = app.listen(port, function () {
    console.log('Listening on port %d', port);
});

Run this command. This will install all the dependencies, which we defined in the package.json file we created previously.
npm install
Start our application and verify the node application works.
node app.js
Open you browser and go to http://localhost:5000

Create azure site

Now that we have the node application working, next step is to move this application to Azure.
Create a new site to where we can deploy node.
azure site create helloworldnode --git
This will create a site and initialise git.

You can use this command to verify azure has been added as a git remote option
git remote show
Add new files to git, commit the changes, and push files to Azure
git add .
git commit -m "created project"
git push azure master
Note if you can’t remember you deployment password you can go to https://manage.windowsazure.com and select you site and press Reset your deployment credentials. You will be prompted for a password when using “git push azure remote” command.
Open the newly created application in a browser, by issuing this command
azure site browse
Your node application should be up and running now on windows azure.

Misc tips for using Azure Tools.

You can type azure to view a list of available commands


azure site list
azure site restart “sitename”
azure site deployment list
azure site deployment redeploy
git status

Happy javascripting :)

Etiketter: ,