云函数怎样请求外部链接/
发布于 5 年前 作者 jie31 5826 次浏览 来自 问答

const cloud = require(‘wx-server-sdk’)

var superagent = require(‘superagent’)

cloud.init()

// 云函数入口函数

exports.main = async (event, context) => {

  superagent

  .get(https://www.baidu.com/s?tn=80035161_2_dg&wd=superagent)

  .end(function(req,res){

    console.log(‘结果’+res)

  })

  console.log(‘3’)

}

日志

START RequestId: a5f0f615-c6e2-11e8-9abf-52540064d067

Event RequestId: a5f0f615-c6e2-11e8-9abf-52540064d067 Event:{"userInfo":{"appId":"wx1ff8c5b9ba3bb104","openId":"oWOHt0MMxScoIvxqKe-GcabQ0R3s"}}

2018-10-03T08:02:18.423Z a5f0f615-c6e2-11e8-9abf-52540064d067 { userInfo: 

   { appId: 'wx1ff8c5b9ba3bb104',

     openId: 'oWOHt0MMxScoIvxqKe-GcabQ0R3s' } }

2018-10-03T08:02:18.424Z a5f0f615-c6e2-11e8-9abf-52540064d067 { callbackWaitsForEmptyEventLoop: [Getter/Setter],

  done: [Function: done],

  succeed: [Function: succeed],

  fail: [Function: fail],

  memory_limit_in_mb: 256,

  time_limit_in_ms: 20000,

  request_id: 'a5f0f615-c6e2-11e8-9abf-52540064d067',

  environ: 'TENCENTCLOUD_SECRETID=AKIDh8bdibed7kOg2sXlMqSTzaeK6PN7VV2w;TENCENTCLOUD_SECRETKEY=2nHZ3U3JVCYQpp3tYxP3sYdUw33seMiZ;TENCENTCLOUD_SESSIONTOKEN=d7175f626718a6b48d4916c44ef1b1a69f32456d40001' }

2018-10-03T08:02:18.425Z a5f0f615-c6e2-11e8-9abf-52540064d067 3

END RequestId: a5f0f615-c6e2-11e8-9abf-52540064d067

Report RequestId: a5f0f615-c6e2-11e8-9abf-52540064d067 Duration:20.079ms Memory:256MB MaxMemoryUsed:0.871094MB

2 回复

const cloud = require('wx-server-sdk')

var superagent = require('superagent')

cloud.init()


// 云函数入口函数

exports.main = async (event, context) => {


return new Promise(function(resolve, reject) {

try {

superagent.get(https://www.baidu.com/s?tn=80035161_2_dg&wd=superagent).end(function(req, res) {

if (res.status == 200) {

return resolve(res);

} else {

return reject(req)

}

})

} catch (e) {

return reject(e)

}

});



}

回到顶部