Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

REST call using node and express succeeds locally but fails once deployed to azure

REST call using node and express succeeds locally but fails once deployed to azure

Problem

I have a pretty basic REST call that is written using Node / express that takes 2 datetimes as parameters(http://localhost:3000/schedules/'2012-06-28T16:00:00'/'2012-06-28T19:00:00') It works fine locally when I run my tests against the node executable directly(on windows and on my mac) but when I deploy it to Azure via git, I get the infamous yellow screen.

The debugging log from azure is empty(never getting to my console.logs in node) so I'm guessing that the error is occurring somewhere in iisnode before it gets routed to the node exe. The call does work fine if I use the following format (http://localhost:3000/schedules/2012-06-28/2012-06-29). I was about to install iisnode to test a little more but I was wondering if anyone else has run into this issue before I delve an deeper.

Here's some partial chunks of code:

routes

app.param('startdate',
function(req, res, next, startdate) {
    req.startdate = startdate;
    next();
});

app.param('enddate',
function(req, res, next, enddate) {
    req.enddate = enddate;
    next();
});

app.get('/schedules/:startdate/:enddate',scheduleController.getSchedulesByStartDateAndEndDate);

scheduleController

exports.getSchedulesByStartDateAndEndDate = function(req, res){
    console.log('getSchedulesByStartDateAndEndDate');
    console.log('StartDate'+req.params.startdate);
    console.log('EndDate'+req.params.enddate);
    ScheduleProvider.getSchedulesByStartDateAndEndDate(req.params.startdate,     req.params.enddate,function(schedules){
        res.send(schedules);
    });
};

scheduleProvider

ScheduleProvider.prototype.getSchedulesByStartDateAndEndDate =     function(startdate,enddate,callback){
console.log(startdate);
console.log(enddate);
var stDate = startdate;
var endDate = enddate;
if (stDate !== undefined) {
    var startDate = new Date(unescape(stDate.toString()).replace(/'/gi, ""));
    endDate = new Date(endDate === undefined ? startDate: unescape(endDate.toString()).replace(/'/gi, ""));
    var sttimes = getTimes(startDate);
    var endtimes = getTimes(endDate);
    console.log(startDate);
    console.log(endDate);
    console.log(sttimes[0]);
    console.log(sttimes[1]);
    console.log(endtimes[0]);
    console.log(endtimes[1]);

    Schedule.find({
        $or: [
        {
            'StartDate': {
                $gte: sttimes[0],
                $lt: endtimes[1]
            }
        },
        {
            'EndDate': {
                $gt: sttimes[0],
                $lte: endtimes[1]
            }
        }
        ]
    }, function(err, schedules){
    callback(schedules);
}).sort('StartDate', 'ascending');
}
};

I'm using mongoose to make the calls but I don't think it's ever getting to the provider or controller code. It's almost like azure is not liking the dates passed in and throwing an exception.

Problem courtesy of: Michael Smith

Solution

When running locally, you are using Node directly to host the application. When running in Windows Azure Web Sites you are using both IIS and IISNode as well as Node. I suspect you are having trouble with IIS handling the single quotes and Node (locally) not doing so. You should try running the site locally in IIS (with IISNode) to see if there is the same problem. Possibly the easiest way to do that is to run the site in the Windows Azure Emulator since you can then use the tooling to deploy your application.

Solution courtesy of: Neil Mackenzie

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

REST call using node and express succeeds locally but fails once deployed to azure

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×