node.js - 获取 http.get Node.js 的前两个字节
问题描述
我需要 AWS lambda 函数中的 http.get 的前两个字节(用于魔术验证)。这是我的代码:
exports.handler = (event, context, callback) => {
var https = require('https');
var url= "https://mail.google.com/mail/u/0/?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
});
推荐阅读
- java - 需要逻辑上的帮助以进行实际操作
- ios - 在 Unity 中使用 ProjectCapabilityManager 向 Xcode 输出添加权利时,CODE_SIGN_ENTITLEMENTS 不正确
- c++ - 计算大型矩阵的行列式 - 应该使用哪些值?
- python-3.x - 如何改进我的错误处理,以便正确处理 Tweepy 中的异常 StopIteration 并且可以继续执行?
- node.js - 在 puppeteer 中处理新标签下载
- java - 如何处理viewpager2中edittext的焦点
- python-3.x - 如何使用我的使用量和量对成本的括号动态计算水费成本?
- apache-spark - 如何根据火花结构化流中的时间戳字段进行重复数据删除和保持最新?
- shopware - 我如何以 Shopware 6 的形式上传拖车图像
- java - 使用 JScrollPane 的正确方法是什么?