得到一个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
});
得到一个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
});