最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

得到一个http.get的Node.js的前两个字节

运维笔记admin9浏览0评论

得到一个http.get的Node.js的前两个字节

得到一个http.get的Node.js的前两个字节

我需要前两个字节http.get(魔验证)的,在AWS lambda函数。这是我的代码:

exports.handler = (event, context, callback) => {
var https = require('https');
var url= "/?ui=2&ik=806f533220&attid=0.1&permmsgid=msg-a:r-8750932957918989452&th=168b03149469bc1f&view=att&disp=safe&realattid=f_jro0gbqh0";


var result= https.get(url , (resp) => {
    let data = '';

    // A chunk of data has been recieved.
    resp.on('data', (chunk) => {
      data += chunk;
    });

    // The whole response has been received. Print out the result.
    resp.on('end', () => {
    });

    }).on("error", (err) => {
       console.log("Error: " + err.message);
    });

    callback(null, '3');// I want to return the first two bytes...
};

有任何想法吗?非常感谢!!

回答如下:

问题是,你是太快调用callback,即你收到resp回调之前。尝试移动回调,例如

resp.on('data', (chunk) => {
  data += chunk;
  var data = // here you may have the data you need, either call the callback temporarily or execute the callback immediately
  callback(null, data);
});

或等待resp到结束:

resp.on('end', () => {
  // pass the temporarily stored data to the callback
  callback(null, data);
});

或者如果resp导致错误:

resp.on("error", (err) => {
   console.log("Error: " + err.message);
   callback(err);  // make sure to let the caller of Lambda know that the request failed
});
发布评论

评论列表(0)

  1. 暂无评论