Poll REST service with Node.js

node.js

Solution

There might be better ways, but I ended up just using event emitters to process the REST response as follows:

var fourSquareGet = {
    host: 'api.foursquare.com',
    port: 443,
    path: '/v2/venues/search?ll=33.88,-119.19&query=burger*',
    method: 'GET'
};
setInterval(function () {
    var reqGet = https.request(fourSquareGet, function (res) {
        var content;

        res.on('data', function (chunk) {
            content += chunk;
        });
        res.on('end', function () {
            // remove 'undefined that appears before JSON for some reason
            content = JSON.parse(content.substring(9, content.length));
            db.checkins.save(content.response.venues, function (err, saved) {
                if (err || !saved) throw err;
            });
            console.info("\nSaved from Foursquare\n");
        });
    });

    reqGet.end();
    reqGet.on('error', function (e) {
        console.error(e);
    });
}, 25000);

However, I'm not sure why I had parse out "undefined" from the JSON I received from foursquare.

Problem

I'm working on a service that would poll Foursquare for certain check-ins every minute or so and save/update the results in a NoSQL database. Is the best approach to wrap an http.request with setInterval and then aggregate the chunked response using the data event emitter? I plan to use the end emitter to parse the JSON and push into a NoSQL DB when the request is complete. Thoughts?

Original source