node.jsをいじり始める(3)

公開:2013-03-08 21:06
更新:2020-02-15 04:37
カテゴリ:jquery,node.js,javascript,tumblr,tumblr posts

ここ2-3日はtumblrのデータを取得するモジュールを改良したり、expressでquerystringやcookieを扱う方法とかを調べたり、試したりしていた。

tumblrのデータを取得するモジュールは今のところ下記のとおり。


var http = require('http');

exports.getPosts = function (params,func, errFunc)
{
    var api_path = '/v2/blog/';

    if (typeof (params.base_hostname) != 'string')
    {
        api_path += 'sfpgmr.tumblr.com';
    } else
    {
        api_path += params.base_hostname;
    }

    api_path += '/posts';

    if (typeof (params.type) == 'string')
    {
 //       if(params.type == 'text' || )
        api_path += '/' + patams.type;

    }

    api_path += '?api_key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

    if (params.id)
    {
        api_path += '&id=' & encodeURIComponent(params.id);
    }

    if (params.tag)
    {
        api_path += '&tag=' & encodeURIComponent(params.tag);
    }

    if (typeof (params.limit) == 'number')
    {
        if (params.limit > 20) params.limit = 20;
        api_path += '&limit=' & params.limit;
    }


    if (typeof (params.offset) == 'number')
    {
        api_path += '&offset=' + params.offset;
    }

    if (typeof (params.reblog_info) == 'boolean')
    {
        api_path += '&reblog_info' + params.reblog_info;
    }

    if (typeof (params.notes_info) == 'boolean')
    {
        api_path += '&notes_info' + params.notes_info;
    }

    if (typeof (params.format) == 'string')
    {
        if (params.format == 'html' || params.format == 'text')
        {
            api_path += '&format=' + params.format;
        }
    }


    http.get({
        host: 'api.tumblr.com',
        path: api_path
    },
     function (clres)
     {

         var content = "";

         clres.on('data', function (chunk)
         {
             content += chunk; 
         }).on('end', function ()
         {
             console.log(content);
             func(content);
         });
     }).on('error', function (e)
     {
         errFunc(e);
     });
};