node 后台ajax文件(同时支持http、https)

var http = require("http"),
    Url = require("url"),
    querystring = require('querystring');

// 默认值
function defaults(url){
    return {
        // 如果返回false可以取消本次请求
        beforeSend: function(req){},
        complete: function(req){},
        data: '', // Object, String
        dataType: 'html',
        error: function(){},
        headers: {}, // {k:v, ...}
        statusCode: {},
        success: function(data){},
        timeout: 10,
        type: 'GET', // GET, POST
        url: url
    };
}

function ajax( settings ){
    // ajax(settings)
    if( typeof settings === "object" ) {
        // 处理默认值继承
         // todo ...

    }
    
    var params = Url.parse(settings.url, true);
    // params 解析出来的参数如下
    // {
    // "protocol":"http:",
    // "slashes":true,
    // "host":"localhost:3000",
    // "port":"3000",
    // "hostname":"localhost",
    // "href":"http://localhost:3000/?d=1",
    // "search":"?d=1",
    // "query":{"d":"1"},
    // "pathname":"/",
    // "path":"/?d=1"
    // }

    var options = {
        host: params.hostname,
        port: params.port || 80, //80:http请求 443:https请求
        path: params.path,
        method: settings.type,
        headers:{
            'Content-Type':'application/x-www-form-urlencoded',
            'Content-Length': settings.data ? settings.data.length : 0
        }
    };

    if(settings.cookie) options.headers['Cookie'] = settings.cookie;

    var req = http.request(options, function(res) {
        var data = '';
        res.on('data', function(chunk) {
            data += chunk;
        }).on('end', function(){
            if(    settings.dataType === "json" ) {
                try {
                    data = JSON.parse(data);
                } catch(e) {
                    data = null;
                }
            }
            settings.success(data);
            settings.complete(req);
        });
    }).on('error', function(e) {
        settings.error(e);
    });

    if( typeof settings.beforeSend === "function" ) {
        if ( !settings.beforeSend(req) ) {
            settings.complete(req);
            req.end();
            return false;
        }
    }

    if( settings.type === "POST" ) {
        req.write(settings.data);
    }

    req.setTimeout(settings.timeout);
    req.end();
}

exports.ajax = ajax;

  

posted @ 2014-04-25 14:59  hitbs228  阅读(418)  评论(0编辑  收藏  举报