Build a Node.js web application using Express on a Windows Azure Cloud Service
Node.js includes a minimal set of functionality in the core runtime. Developers often use 3rd party modules to provide additional functionality when developing a Node.js application. In this tutorial you will create a new application using the Express module, which provides an MVC framework for creating Node.js web applications.
A screenshot of the completed application is below:
Create a Cloud Service Project
Perform the following steps to create a new cloud service project named 'expressapp':
From the Start Menu or Start Screen, search for Windows Azure PowerShell. Finally, right-click Windows Azure PowerShell and select Run As Administrator.
To successfully complete this section, you must have a working installation of Node.js and the Windows Azure SDK for Node.js for your platform.
Install the Windows Azure SDK for Node.js: Windows installer | Mac installer | Linux download
If you are using Mac or Linux, install Node.js from http://nodejs.org. The Windows installer already includes Node.js.
Change directories to the c:\node directory and then enter the following commands to create a new solution named expressapp and a web role named WebRole1:
PS C:\node> New-AzureServiceProject expressapp
PS C:\Node> Add-AzureNodeWebRole
Install the Express module by issuing the following command:
PS C:\node\expressapp> npm install express -g
The output of the npm command should look similar to the result below.
Change directories to the WebRole1 directory and use the express command to generate a new application:
PS C:\node\expressapp\WebRole1> express
You will be prompted to overwrite your earlier application. Enter y or yes to continue. Express will generate the app.js file and a folder structure for building your application.
Delete the server.js file and then rename the generated app.js file to server.js.
PS C:\node\expressapp\WebRole1> del server.js
PS C:\node\expressapp\WebRole1> ren app.js server.js
To install additional dependencies defined in the package.json file, enter the following command:
PS C:\node\expressapp\WebRole1> npm install
Use the following command to run the application in the Windows Azure emulator:
PS C:\node\expressapp\WebRole1> Start-AzureEmulator -launch
Modifying the View
Now modify the view to display the message "Welcome to Express in Windows Azure".
Enter the following command to open the index.jade file:
PS C:\node\expressapp\WebRole1> notepad views/index.jade
Jade is the default view engine used by Express applications. For more information on the Jade view engine, see http://jade-lang.com.
Modify the last line of text by appending in Windows Azure.
Save the file and exit Notepad.
Refresh your browser and you will see your changes.
After testing the application, use the Stop-AzureEmulator cmdlet to stop the emulator.
Publishing the Application to Windows Azure
In the Windows Azure PowerShell window, use the Publish-AzureServiceProject cmdlet to deploy the application to a cloud service
PS C:\node\expressapp\WebRole1> Publish-AzureServiceProject -ServiceName myexpressapp -Location "East US" -Launch
Once the deployment operation completes, your browser will open and display the web page.