node 请求

入口文件调用

var _http = require('./http.js');
require('./login.js')(app, _http);

http.js文件

var http = require('http');
var https = require('https');
var util = require('util');

process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; // https请求

/*
 * GET
 * url 访问路径
 *      '/a/company/list?sid=sid'
 * fun 回调方法
 *      function(cont),cont返回的数据
 */
exports.GET = function(url, fun) {
    var cont = '',
        url = 'http://tpos.yingzixia.com' + url;
    http.get(url, function(res) {
        res.setEncoding('utf-8');
        res.on('data', function(chunk) { cont += chunk; });
        res.on('end', function() {
            fun(cont);
        });
    }).on('error', function(err) {
        console.log("Got error: " + err.message);
    });
};

/*
 * POST
 * postData 传递参数
 *      {
 *          'account': name,
 *          'passwd': md5(passwd)
 *      }
 * url 访问路径
 *      '/a/app/login'
 * fun 回调方法
 *      function(cont),cont返回的数据
 */
exports.POST = function(postData, url, fun) {
    var querystring = require('querystring');
    postData = querystring.stringify(postData);
    var cont = '',
        options = {
            hostname: 'tpos.yingzixia.com',
            path: url,
            port: 80,
            method: 'POST',
            headers: {
                'Content-Length': postData.length,
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        },
        req = http.request(options, function(res) {
            res.on('data', function(chunk) { cont += chunk; });
            res.on('end', function() {
                try {
                    cont = JSON.parse(cont);
                    fun(cont);
                } catch (e) {
                    fun(cont);
                }
            });
        });
    req.on('error', function(e) {
        console.log('Error: ' + e.messsage);
    });
    req.write(postData);
    req.end();
};

/*
 * HTTPS POST请求
 */
exports.POST2 = function(postData, url, fun) {
    var querystring = require('querystring');
    postData = querystring.stringify(postData);
    var cont = '',
        options = {
            hostname: 'tpos.yingzixia.com',
            path: url,
            port: 443,
            method: 'POST',
            headers: {
                'Content-Length': postData.length,
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        },
        req = https.request(options, function(res) {
            res.on('data', function(chunk) { cont += chunk; });
            res.on('end', function() {
                try {
                    cont = JSON.parse(cont);
                    fun(cont);
                } catch (e) {
                    fun(cont);
                }
            });
        });
    req.on('error', function(e) {
        console.log('Error: ' + e.messsage);
    });
    req.write(postData);
    req.end();
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值