node + express实现没有ca认证https跨域

2019-01-22  本文已影响0人  小草_fdba

全部接口实现跨域,某个接口则修改*为url

app.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By", ' 3.2.1')
    if (req.method == "OPTIONS") res.send(200);/*让options请求快速返回*/
    else next();
});

经测试跨域是可以的,但是浏览器没有给https授权的话会有没有初次认证的错误。但是这种办法应该是对http和认证过的https有效的

之后尝试第二种方法,在node后台接口中向其他服务器请求https接口

   var data_str;
   var yunDunUrl = 'https://' + cip + '******';
    
    var options = url.parse(yunDunUrl);
    var data_str = '';
    options.method = 'GET';
    options.headers = {
        'Content-Type': 'application/json'
    };
    options.rejectUnauthorized = false;//拒绝认证,若被设置为true,会根据提供的 CA 列表来验证服务器证书。当验证失败时,会触发error事件;err.code包含了一个 OpenSSL 错误码。若为false则不认证
   var https_req = https.request(options, function (resp) {
        resp.on('data', function (chunk) {
            data_str += chunk;
        });

        // The whole response has been received. Print out the result.
        resp.on('end', function () {
            var req_data = JSON.parse(data_str);
        });
    });
    https_req.on('error', function (e) {
        
    });
    https_req.end();
上一篇 下一篇

猜你喜欢

热点阅读