首页 > 解决方案 > Lambda 本地和 HTTP 调用

问题描述

我在node.js中有一个函数,需要在AWS中作为lambda托管。但是在我们托管它之前,我们需要检查该功能是否正常工作。我一直在尝试使用lambda-local运行该函数。但挑战是,我的逻辑中有外部 HTTP 调用。我为此使用Axios,但我得到的响应为undefined。有什么方法可以访问那些外部 HTTP 端点并获得响应?下面是命令行日志。提前致谢。

D:\Billing_&_Consumption\P2D-Phase2 - Billings\POC\scripts\controller>lambda-local -l DocumentsHandler.js -h getDocumentsList -e event.js
warning Using both auth systems: aws_access_key/id and secret_access_token !
info: START RequestId: 155ee184-1a7b-464a-eeac-d5593bde5abe

Response: undefined
TypeError: Cannot read property 'access_token' of undefined
    at Object.DocumentsController.getDocumentsList (D:\Billing_&_Consumption\P2D-Phase2 - Billings\POC\scripts\controller\DocumentsController.js:26:87)
    at Object._executeSync (C:\Users\429732\AppData\Roaming\npm\node_modules\lambda-local\lib\lambdalocal.js:169:47)
    at Object._execute [as execute] (C:\Users\429732\AppData\Roaming\npm\node_modules\lambda-local\lib\lambdalocal.js:40:22)
    at C:\Users\429732\AppData\Roaming\npm\node_modules\lambda-local\bin\lambda-local:133:21
    at Object.<anonymous> (C:\Users\429732\AppData\Roaming\npm\node_modules\lambda-local\bin\lambda-local:169:3)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
error: End - Error
error: ------
error: {
        "errorMessage": "Cannot read property 'access_token' of undefined",
        "errorType": "TypeError",
        "stackTrace": [
                "Object.DocumentsController.getDocumentsList (D:\\Billing_&_Consumption\\P2D-Phase2 - Billings\\POC\\scripts\\controller\\DocumentsController.js:26:87)",
                "Object._executeSync (C:\\Users\\429732\\AppData\\Roaming\\npm\\node_modules\\lambda-local\\lib\\lambdalocal.js:169:47)",
                "Object._execute [as execute] (C:\\Users\\429732\\AppData\\Roaming\\npm\\node_modules\\lambda-local\\lib\\lambdalocal.js:40:22)",
                "C:\\Users\\429732\\AppData\\Roaming\\npm\\node_modules\\lambda-local\\bin\\lambda-local:133:21",
                "Object.<anonymous> (C:\\Users\\429732\\AppData\\Roaming\\npm\\node_modules\\lambda-local\\bin\\lambda-local:169:3)",
                "Module._compile (module.js:652:30)",
                "Object.Module._extensions..js (module.js:663:10)",
                "Module.load (module.js:565:32)",
                "tryModuleLoad (module.js:505:12)",
                "Function.Module._load (module.js:497:3)"
        ]
}
error: ------
error: Lambda failed in 135ms.

我可以为您提供 axios 调用及其解决和拒绝

public getAccessToken(): AxiosPromise<any> {
    return axios({
        method: 'post',
        url: `${URL}`,
        data: this.data,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    });
}

服务.ts

this.documentStoreBroker.getAccessToken()
    .then((token: any) => {
        console.log(token);
        return new ADSAccessToken(token);
    })
    .catch(error => {
        console.log(error);
        throw new InternalServerError('99x100');
    });
}

标签: javascriptnode.jsaws-lambda

解决方案


首先,您应该尝试通过Postman访问令牌的端点。您可以验证端点的设置是否正确,然后对 axios 使用相同的设置。

如果可行,您可以继续使用 lambda 并使用Nock模拟您期望的响应(例如您在邮递员中得到的响应)。Nock 拦截 http 调用,这样您就不会再碰到真正的端点,您可以专注于其余的逻辑。

然而,'noocked' 代码不应该进入生产环境,因为它会阻止到达真正的端点,所以我建议使用某种测试环境来编写不影响生产代码的测试。


推荐阅读