最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

如何在node.js lambda中使用请求模块

IT培训 admin 5浏览 0评论

如何在node.js lambda中使用请求模块

我正在尝试将AWS Lambda中的请求模块用于Node.js,但效果不理想。

这是我到目前为止拥有的功能:

const request = require('request');
url = ''

exports.handler = async (event) => {

    request(url, (error,res,body) => {
        console.log(error);    //this is not printing
        console.log(res);      //this is not printing
        console.log(body);     //this is not printing
        console.log('Come on!!'); //this is not printing
    });

    console.log(url); //This is the only thing printing, you can see in the Function Logs below, second line.

};

这是我得到的答复:

Response:
null

Request ID:
"3e46f401-f26d-435d-90be-ac848c6c3a39"

Function Logs:
START RequestId: 3e46f401-f26d-435d-90be-ac848c6c3a39 Version: $LATEST
2019-10-14T08:06:23.755Z    3e46f401-f26d-435d-90be-ac848c6c3a39    INFO    
END RequestId: 3e46f401-f26d-435d-90be-ac848c6c3a39
REPORT RequestId: 3e46f401-f26d-435d-90be-ac848c6c3a39  Duration: 368.93 ms Billed Duration: 400 ms Memory Size: 128 MB Max Memory Used: 92 MB  Init Duration: 461.54 ms

我想知道为什么我没有从request方法内部得到任何响应。我在笔记本电脑上尝试过,并且处理程序之外的部分代码运行正常。

我将项目作为zip文件上传,因此带有请求模块的node_modules文件夹确实存在。我注意到使用http模块而不是request的一些解决方案,但是我想先了解为什么它不起作用,然后再转向其他解决方案。

回答如下:

您的函数被标记为async,但是您正在使用回调来处理响应。因此,awaited处于关闭状态。

这意味着您的函数将返回一个已兑现的隐含承诺。这使lambda停止。因此,要么:

A)使用async / await编写代码,这可能需要request-promise或类似的代码:

exports.handler = async event => {
    const res = await promisifiedRequest(url);
    console.log(res.statusCode);
    console.log(url);
};

B)将函数从async/await转换为使用回调,如下所示:

exports.handler = (event, context, callback) => {
    const res = request(url, (error, res, body) => {
        console.log('Come on!!');
        callback(null, res.statusCode);
    });
    console.log(url);
};

如何在node.js lambda中使用请求模块

我正在尝试将AWS Lambda中的请求模块用于Node.js,但效果不理想。

这是我到目前为止拥有的功能:

const request = require('request');
url = ''

exports.handler = async (event) => {

    request(url, (error,res,body) => {
        console.log(error);    //this is not printing
        console.log(res);      //this is not printing
        console.log(body);     //this is not printing
        console.log('Come on!!'); //this is not printing
    });

    console.log(url); //This is the only thing printing, you can see in the Function Logs below, second line.

};

这是我得到的答复:

Response:
null

Request ID:
"3e46f401-f26d-435d-90be-ac848c6c3a39"

Function Logs:
START RequestId: 3e46f401-f26d-435d-90be-ac848c6c3a39 Version: $LATEST
2019-10-14T08:06:23.755Z    3e46f401-f26d-435d-90be-ac848c6c3a39    INFO    
END RequestId: 3e46f401-f26d-435d-90be-ac848c6c3a39
REPORT RequestId: 3e46f401-f26d-435d-90be-ac848c6c3a39  Duration: 368.93 ms Billed Duration: 400 ms Memory Size: 128 MB Max Memory Used: 92 MB  Init Duration: 461.54 ms

我想知道为什么我没有从request方法内部得到任何响应。我在笔记本电脑上尝试过,并且处理程序之外的部分代码运行正常。

我将项目作为zip文件上传,因此带有请求模块的node_modules文件夹确实存在。我注意到使用http模块而不是request的一些解决方案,但是我想先了解为什么它不起作用,然后再转向其他解决方案。

回答如下:

您的函数被标记为async,但是您正在使用回调来处理响应。因此,awaited处于关闭状态。

这意味着您的函数将返回一个已兑现的隐含承诺。这使lambda停止。因此,要么:

A)使用async / await编写代码,这可能需要request-promise或类似的代码:

exports.handler = async event => {
    const res = await promisifiedRequest(url);
    console.log(res.statusCode);
    console.log(url);
};

B)将函数从async/await转换为使用回调,如下所示:

exports.handler = (event, context, callback) => {
    const res = request(url, (error, res, body) => {
        console.log('Come on!!');
        callback(null, res.statusCode);
    });
    console.log(url);
};

发布评论

评论列表 (0)

  1. 暂无评论