Shaun Xu

The Sheep-Pen of the Shaun


News

logo

Shaun, the author of this blog is a semi-geek, clumsy developer, passionate speaker and incapable architect with about 10 years experience in .NET. He hopes to prove that software development is art rather than manufacturing. He's into cloud computing platform and technologies (Windows Azure, Aliyun) as well as WCF and ASP.NET MVC. Recently he's falling in love with JavaScript and Node.js.

Currently Shaun is working at IGT Technology Development (Beijing) Co., Ltd. as the architect responsible for product framework design and development.

MVP

My Stats

  • Posts - 94
  • Comments - 335
  • Trackbacks - 0

Tag Cloud


Recent Comments


Recent Posts


Archives


Post Categories



It has been long time didn’t blog anything since last November. There are many exciting things happened during this period. I attend Microsoft TechED 2012 China and provided a session about Node.js and Windows Azure. I also provided a small but funny session at the Microsoft China Community New Year Party on this January. Then I enjoyed Chinese New Year at Lvshun, a quiet and beautiful city aside Bohai Bay with my wife, where her hometown is.

But I decided to write something when I read this. On February, Microsoft announced that the Windows Azure Store had just expanded availability to new 25 markets, includes Hong Kong SAR, which is the country/region for my Windows Azure account. This means I can try this fantastic feature with my subscription.

 

Windows Azure Store

Windows Azure Store is a brand new service provided by Windows Azure platform. It likes a service market, where you can buy services provided by many third-party publishers. With Windows Azure Store, as a developer we can:

1. Choose from popular app services including: AppDynamics (monitoring and scaling as a service), New Relic (performance management and monitoring), SendGrid (email delivery as a service) and more.

2. Find the data your app needs from data providers like: Dun & Bradstreet (business insights), Loqate (Worldwide address verification and cleansing service), StrikeIron (Phone number verification service; Sales and use tax rates lookup service), and more.

3. Connect and manage it all from the developer portal: The integrated store UI makes it easy to find, buy, manage the services and data you need directly from the portal.  Integrated billing means you store purposes are added directly to your Windows Azure account.  We also provide connection strings and sample code and help get you started fast, as well as usage data from directly within the Windows Azure management portal.

Furthermore, there are other benefit of using Windows Azure Store.

1. It utilizes similar UI as Windows Azure portal, which means it’s very easy to select, view and purchase.

2. All services are located in Windows Azure data centers. This means we can choose the best region to minimize our cost.

3. We can view the billing report in our Windows Azure subscription management page.

4. Almost all services in Windows Azure Store provides multiple subscription plan and most of them have free subscription.

 

Windows Azure Store and Windows Azure Marketplace

If you had playing with Windows Azure a while, you might heard that there’s another feature in Windows Azure called Windows Azure Marketplace, where you can buy data and applications published from other venders. It sounds very similar as Windows Azure Store. So what’s different between them? Will Windows Azure Marketplace be replaced by Windows Azure Store.

Here I would like to quote some clarification from Ryan McGee at Microsoft.

The Windows Azure Store and Windows Azure Marketplace will continue to co-exist; there is not a plan to converge them. The reason is, they serve different audiences and they contain different kinds of products.

The Windows Azure Store makes it easy to find and purchase add-ons, and use them to create your applications. While the Windows Azure Marketplace is an online market where ISVs and data publishers can publish and sell finished applications and premium datasets to business customers.

So my understanding is, if you are going to build your application on Windows Azure and want to leverage some services Windows Azure doesn’t provide, you’d better look for the Windows Azure Store. But if you are going to use some data, or you want to find some applications, you can seek in Windows Azure Marketplace.

 

Node.js + MongoDB by Using Windows Azure Store

Now let’s take a look on how to use Windows Azure Store. In this post I would like to demonstrate how to build a simple website by using Node.js and MongoDB.

 

As you know, Windows Azure doesn’t offer MongoDB. Before Windows Azure Store was announced, we have to run our own MongoDB service in a work role or in a virtual machine. This is OK but we have to take care for installation, configuration, scaling, etc.. Now we have Windows Azure Store so let’s check if there’s some MongoDB related services. Open the Windows Azure portal, from the navigate click ADD-ONS, and then click the PURCHASE ADD-ONS.

image

Now we can see the services in the Windows Azure Store. Scroll down the list and we will find a service named MongoLab, which is MongoDB-as-a-Service. Click next icon at the bottom right of the dialog.

image

Then we can choose the purchase plan. Currently there’s only one free plan for MongoLab which includes one 0.5G MongoDB. Select the valid Windows Azure subscription, specify our MongoDB name and choose the region. This is where our MongoDB will be provisioned.

image

Then in the last step, review our selection and clicked the PURCHASE icon.

If you found in the last step it said “We’re sorry, we cannot estimate the bill at this time.” this because you had registered Windows Azure Marketplace with different country/region. To solve this issue, you can log in the Windows Azure Marketplace and change the country/region to the one your Windows Azure subscription is.

After several seconds our MongoDB will be established in that region.

image

The connection information can be retrieved by clicking the CONNECTION INFO button at the bottom. We will use this information in our Node.js application later.

image

 

Next, we will create a new Windows Azure Website and write our Node.js code. Ref how to use Windows Azure Website and publish through local GIT, please refer my previous post. Just ensure that we specify the same region when creating website so that the network traffic will not be billed.

image

After cloned it to local disk we will add the .gitignore and package.json files. The content would be like this.

   1: node_modules
   1: {
   2:   "name": "nodemongotest",
   3:   "version": "1.0.0",
   4:   "dependencies": {
   5:     "mongoose": "~3.5.7",
   6:     "express": "~3.1.0"
   7:   }
   8: }

And then install “express” and “mongoose” modules through NPM command. “mongoose” is the module we are going to use to connect our MongoDB.

For more information about the “mongoose” module please refer its website.

Then, create our Node.js source code file named “server.js” and paste the code from below. Make sure to replace the MongoDB connection string by your MongoDB connection information in line 16.

   1: (function () {
   2:     "use strict";
   3:  
   4:     var app = require("express")();
   5:     var server = require("http").createServer(app);
   6:  
   7:     var mongoose = require("mongoose");
   8:     var Schema = mongoose.Schema;
   9:     var ObjectId = Schema.ObjectId;
  10:  
  11:     var guestSchema = new Schema({
  12:         client_ip: String,
  13:         request_date: String
  14:     });
  15:  
  16:     var conn = mongoose.createConnection("mongodb://TestMongoDB:wL5JJFkRPtYFS96_BbdustMrcgijw3C17B0rTdStqh8-@ds045107.mongolab.com:45107/TestMongoDB");
  17:     // var conn = mongoose.createConnection("mongodb://127.0.0.1/nodeandmongo-db");
  18:     var GuestItem = conn.model("GuestItem", guestSchema);
  19:  
  20:     var guestCount = function (req, callback) {
  21:         // retrieve the client request ip and date
  22:         var request_date = (new Date()).toString();
  23:         var request_ip = req.header('x-forwarded-for') || req.connection.remoteAddress;;
  24:         // try to insert into mongo
  25:         var guest = new GuestItem;
  26:         guest.client_ip = request_ip;
  27:         guest.request_date = request_date;
  28:         guest.save(function (error) {
  29:             if (error) {
  30:                 callback(error, null);
  31:             }
  32:             else {
  33:                 // retrieve all items in mongo
  34:                 GuestItem.find(function (error, items) {
  35:                     callback(error, items);
  36:                 });
  37:             }
  38:         });
  39:     }
  40:  
  41:     // routing
  42:     app.get("/", function (req, res) {
  43:         guestCount(req, function (error, guests) {
  44:             if (error) {
  45:                 res.send(200, error);
  46:             }
  47:             else {
  48:                 res.send(200, guests);
  49:             }
  50:         })
  51:     });
  52:  
  53:     server.listen(process.env.PORT || 12345);
  54: })();

This code will create a connection in our MongoDB named GuestItem. Then it will log the request client IP and request date once the website was viewed, and show the records from MongoDB.

Publish the code to Windows Azure Website and have a try, we can see that the records grown when we refresh the page.

image

 

Manage MongoDB

We are using MongoLab from Windows Azure Store in our application. But in Windows Azure portal there’s no management page for us to view the collections and documents. But we can view them in MongoLab’s website. Open the Windows Azure portal select the MongoLab in ADD-ONS section, and then click the MANAGE button.

image

Another web page will be popped up. It’s the management page provided by MongoLab. And from here we can see our collection and documents in our MongoDB.

image

And we can add, update and delete collections, documents in this page.

image

 

Summary

In this post I introduced Windows Azure Store, the different with the existing Windows Azure Marketplace. I Also demonstrate on how to use MongoLab from Windows Azure Store, working with Node.js.

There are many other services in Windows Azure Store currently. Below are some of them we thought might be useful.

ClearDB MySQL database-as-a-service, which is being used in Windows Azure Website feature.
MongoLab MongoDB database-as-a-service.
New Relic All-in-one web application performance tool that lets you see performance from the end user experience, through servers, and down to the line of app code.
Scheduler Time-based eventing made simple. Schedule CRON jobs with ease.
SendGrid Cloud-based email service that delivers email on behalf of companies to increase deliverability and improve customer communications.

And if you are interested in hosting and sale our great service in Windows Azure Store, you can contact wastorepartners@microsoft.com.

 

Hope this helps,

Shaun

All documents and related graphics, codes are provided "AS IS" without warranty of any kind.
Copyright © Shaun Ziyan Xu. This work is licensed under the Creative Commons License.

Comments

No comments posted yet.
Post A Comment
Title:
Name:
Email:
Comment:
Verification: