Just a quick story/solution today so that I don't completely neglect this blog. I recently changed a little project from using Azure to using AppFog, and everything was going fine until I started having some random connection issues with MongoDB. After deployment everything would work fine for a while, but then a few minutes later I wouldn't be able to connect the the database and I just got a random error or timeout.
After scratching my head for a while, Googling around for a solution, and changing my DB layer implementation a few times, I was out of ideas. Then I started to look into my configuration. I have my DB hosted at mongolab and I hadn't updated the hosting location - it was still hosted on Azure, but my AppFog site was hosted on AWS. I didn't think this would be a problem, but I cloned my DB and hosted it on AWS, and then everything started working perfectly.
So if anyone happens to have this problem, and you somehow manage to end up at this site - just make sure that your DB is hosted at the same place as your app.
Showing posts with label Azure. Show all posts
Showing posts with label Azure. Show all posts
Saturday, February 16, 2013
Monday, December 31, 2012
TypeScript Node.js Development Part 6 - AppFog Deployment
It's been a long and exciting journey, but we now have a pretty good foundation of a Node.js app, tested and debuggable, deployed to Azure. That's great, but I quickly ran into a problem. Apparently the way that Azure Web Site processes are handled means that long-running processes are not allowed, and are automatically killed after about half an hour. What I was trying to write required a timer going off at a certain interval indefinitely, but there was just no way to get this to work in an Azure Web Site. My only option if I was going to stick with Azure was to use a Cloud Service rather than a Web Site, but that would take me out of the free tier, which is quite unacceptable.
After a bit of searching around, I found out about a service called AppFog. AppFog is a "Platform as a Service" or PaaS provider, which basically means that, like Azure, they take out all the hassle of deploying code to "The Cloud". The way it works is simple, you sign up for an account, and create an app by choosing from a template (Node app, etc) and a hosting provider (AWS, etc). Behind the scenes, AppFog will provision the host and deploy the template code and in minutes you'll have a skeleton website ready to go. And best of all there is a generous free tier.
Once set up, all you need to do is download the template source code from the AppFog console, overwrite it with the code that we've been using in Azure, update the environment variables again using the console, then upload the code using a few simple commands:
There isn't the nice git integration and ability to view and rollback deployments like in Azure, but it does allow long-running processes, and it seems to be a bit easier to use and much more adaptable than Azure.
And I'll leave this series of posts at that, hopefully someone has learned something from all this and decides to give TypeScript Node.js development a go, it's been fun!
Previously: TypeScript Node.js Development Part 5 - Unit Tests and Debugging.
After a bit of searching around, I found out about a service called AppFog. AppFog is a "Platform as a Service" or PaaS provider, which basically means that, like Azure, they take out all the hassle of deploying code to "The Cloud". The way it works is simple, you sign up for an account, and create an app by choosing from a template (Node app, etc) and a hosting provider (AWS, etc). Behind the scenes, AppFog will provision the host and deploy the template code and in minutes you'll have a skeleton website ready to go. And best of all there is a generous free tier.
Once set up, all you need to do is download the template source code from the AppFog console, overwrite it with the code that we've been using in Azure, update the environment variables again using the console, then upload the code using a few simple commands:
gem install af af login af update [sitename]
There isn't the nice git integration and ability to view and rollback deployments like in Azure, but it does allow long-running processes, and it seems to be a bit easier to use and much more adaptable than Azure.
And I'll leave this series of posts at that, hopefully someone has learned something from all this and decides to give TypeScript Node.js development a go, it's been fun!
Previously: TypeScript Node.js Development Part 5 - Unit Tests and Debugging.
Saturday, December 22, 2012
TypeScript Node.js Development Part 2 - Standard Packages and Azure Deployment
In Part 1 I went through the process of making a basic Node.js app using TypeScript in Visual Studio. In this post, I'll make the app a bit less basic, and deploy it to Azure (a.k.a. "THE CLOUD").
To make this server a bit easier to work with, I'll use three Node.js packages - express for routing etc, ejs for templating, and jade for confusing-looking HTML that seems like it's an interesting thing to try out. Since we're using Node.js, adding these packages is easy, just create a package.json file alongside your app.ts file:
Then run "npm install" in the app folder.
This pulls in the packages, but TypeScript doesn't know how the express code is defined until we use the definition provided by node-definitions (which we covered in part 1). You could just add "/// <reference path="./node-definitions/express.d.ts" />" to the top of your app.ts file, along with the node.d.ts reference, but a nicer way of doing this is to separate all the definition references to another file, app.d.ts:
I won't go into details about express, ejs, or jade, firstly because I don't really understand it in-depth yet, and secondly because I think the code is pretty self-explanatory. The new app.ts, with some sample get and post URLs, looks like this:
We also have a separated route file in routes/index.ts - using routes specified inline or in external files is a matter of preference but I've included both just so you can see how it works:
And our jade "HTML" looks like this:
views/index.jade
views/layout.jade
Finally we have some basic css (public/css/style.css) that I must have gotten from some example code somewhere:
After all of this code, you should now be able to build the project and run "node app", then test out the various URLs and check out how the jade template is translated to HTML.
Now we can move onto something pretty awesome, deploying this code to Azure - which has a free tier that allows you to run up to 10 web sites for free. The Azure team has a really good tutorial that goes through setting up a new Azure Node.js website and getting it ready for git deployment, and they describe the process a lot better that I can, so go ahead and read through that, but instead of using their sample Node.js code, use the code that we have just written.
If you're back, hopefully you've managed to deploy this code to Azure, if so well done! The only thing I want to add is that if you're like me you don't like the idea of publishing a whole lot of files that are not needed for the web server. To avoid this, you can make a .gitignore file that includes things like the node definitions and useless dll files, as well as local copies of the node modules (since Azure will automatically npm install your app once deployed):
So there you have it, Node.js code, written in TypeScript, built using Visual Basic, running on Azure. This post was mainly just code or links and not much instructions, but hopefully it is useful for someone. Since this is starting to involve a bit of code, I've made a Visual Studio template so you can quickly make a new project with everything set up for you. However, things like node-definitions and possibly other packages will be a bit out of date and may need to be updated.
The next part will cover getting a basic front-end framework up and running.
Previously: TypeScript Node.js Development Part 1 - Getting Started, next: TypeScript Node.js Development Part 3 - Twitter Bootstrap
To make this server a bit easier to work with, I'll use three Node.js packages - express for routing etc, ejs for templating, and jade for confusing-looking HTML that seems like it's an interesting thing to try out. Since we're using Node.js, adding these packages is easy, just create a package.json file alongside your app.ts file:
{
"name": "package-name"
, "version": "0.0.1"
, "private": true
, "dependencies": {
"express": "2.5.8"
, "ejs": ">= 0.5.0"
, "jade": ">= 0.0.1"
}
}
Then run "npm install" in the app folder.
This pulls in the packages, but TypeScript doesn't know how the express code is defined until we use the definition provided by node-definitions (which we covered in part 1). You could just add "/// <reference path="./node-definitions/express.d.ts" />" to the top of your app.ts file, along with the node.d.ts reference, but a nicer way of doing this is to separate all the definition references to another file, app.d.ts:
/// <reference path="./node-definitions/node.d.ts" /> /// <reference path="./node-definitions/express.d.ts" />And reference "app.d.ts" at the top of all your TypeScript files. This way, you can add references to your app.d.ts file as required, and the definitions will be available in all your source code files.
I won't go into details about express, ejs, or jade, firstly because I don't really understand it in-depth yet, and secondly because I think the code is pretty self-explanatory. The new app.ts, with some sample get and post URLs, looks like this:
///<reference path='app.d.ts' />
import http = module("http")
import url = module("url")
import routes = module("./routes/index")
import express = module("express")
var app = express.createServer();
var port = process.env.PORT || 1337;
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Routes
app.get('/', routes.index);
app.get('/testUrl', function(req, res) {
console.log('test url ' + req.query['testQS']);
res.send('ok', 200);
});
app.get('/testUrl/:folder', function(req, res) {
console.log('testing folder ' + req.params.folder);
res.send('ok', 200);
});
app.post('/testPost/:userid/newboard', function(req, res) {
console.log('testing post ' + req.params.userid + ', ' + req.param('postdata'));
});
app.listen(port, function(){
console.log("Express server listening on port %d in %s mode", port, app.settings.env);
});
export var App = app;
We also have a separated route file in routes/index.ts - using routes specified inline or in external files is a matter of preference but I've included both just so you can see how it works:
///<reference path='app.d.ts' />
import express = module("express")
export function index(req: express.ExpressServerRequest, res: express.ExpressServerResponse){
res.render('index', { title: 'Page Title', testArray: ["1", "2", "3", "4"] })
};
And our jade "HTML" looks like this:
views/index.jade
h1= title
p Welcome to #{title}
ul
- each test in testArray
li
a(href= "/user/"+test)
b= test
views/layout.jade
!!!
html
head
title PageTitle
link(rel='stylesheet', href='/css/style.css')
body
#header
script(src='http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.min.js')
#container!= body
Finally we have some basic css (public/css/style.css) that I must have gotten from some example code somewhere:
html { background-color: #f9f9f9; margin: 0; padding: 0; }
body { margin: 0 auto; padding: 0; font-family:"Segoe UI","HelveticaNeue-Light", sans-serif; font-weight:200;}
h1, h2, p, summary, footer, li { line-height: 170%; }
h1, h2 { border-bottom: 1px solid #aaa; font-family:"Segoe UI Light","HelveticaNeue-UltraLight", sans-serif; font-weight:100; }
h2 { font-size: 16pt; }
p { margin: 1em 20px 0 20px; }
ul { margin-top: 1em; }
#footer { font-style: italic; color: #999; text-align: center; padding: 1em 0 2em 0; margin-top: 1em; font-size: 80%; }
em { letter-spacing: 1px; }
li { margin-left: 1em; }
After all of this code, you should now be able to build the project and run "node app", then test out the various URLs and check out how the jade template is translated to HTML.
Now we can move onto something pretty awesome, deploying this code to Azure - which has a free tier that allows you to run up to 10 web sites for free. The Azure team has a really good tutorial that goes through setting up a new Azure Node.js website and getting it ready for git deployment, and they describe the process a lot better that I can, so go ahead and read through that, but instead of using their sample Node.js code, use the code that we have just written.
If you're back, hopefully you've managed to deploy this code to Azure, if so well done! The only thing I want to add is that if you're like me you don't like the idea of publishing a whole lot of files that are not needed for the web server. To avoid this, you can make a .gitignore file that includes things like the node definitions and useless dll files, as well as local copies of the node modules (since Azure will automatically npm install your app once deployed):
bin/ obj/ node_modules/ node-definitions/ [appname].csproj [appname].csproj.user [appname].sln [appname].v11.suo
So there you have it, Node.js code, written in TypeScript, built using Visual Basic, running on Azure. This post was mainly just code or links and not much instructions, but hopefully it is useful for someone. Since this is starting to involve a bit of code, I've made a Visual Studio template so you can quickly make a new project with everything set up for you. However, things like node-definitions and possibly other packages will be a bit out of date and may need to be updated.
The next part will cover getting a basic front-end framework up and running.
Previously: TypeScript Node.js Development Part 1 - Getting Started, next: TypeScript Node.js Development Part 3 - Twitter Bootstrap
Subscribe to:
Posts (Atom)