代码之家  ›  专栏  ›  技术社区  ›  AmazingDayToday

如何从AWS Lambda(Node.js)中的处理程序调用module.exports

  •  0
  • AmazingDayToday  · 技术社区  · 7 年前

    “index.handler”调用index.js中的exports.handler。

    exports.handler = (username, password) => {
        ...
    }
    

    但如果代码是这样的:

    module.exports = (username, password) => {
        ...
    }
    

    我怎么称呼它?我没试过像这样的 module.exports , module.handler 等等。

    3 回复  |  直到 7 年前
        1
  •  16
  •   Kalev    7 年前

    AWS Lambda希望您的模块导出包含处理程序函数的对象。然后在Lambda配置中声明包含模块的文件和处理程序函数的名称。

    在Node.js中导出模块的方式是通过 module.exports 财产。函数的返回值 require 模块.exports 属性。

    exports 模块.exports 出口 ,而使用 模块.exports ,因为其他代码可能会覆盖 ,导致意外行为。

    handler . 但是,在第二个代码示例中,您的代码只导出一个函数。因为这与AWS Lambda的API不匹配,所以这不起作用。

    考虑以下两个文件:export\u object.js和export\u function.js:

    // export_object.js
    
    function internal_foo () {
        return 1;
    }
    
    module.exports.foo = internal_foo;
    

    // export_function.js
    
    function internal_foo () {
        return 1;
    }
    
    module.exports = internal_foo;
    

    当我们跑的时候 require('export_object.js') 我们得到一个只有一个函数的对象:

    > const exp = require('./export_object.js')
    undefined
    > exp
    { foo: [Function: internal_foo] }
    

    require('export_function.js') ,我们得到一个函数:

    > const exp = require('./export_funntion.js')
    undefined
    > exp
    [Function: internal_foo]
    

    当您将AWS Lambda配置为运行一个名为 处理程序 index.js ,这里是调用函数时Amazon的近似值:

    const handler_module = require('index.js);
    return handler_module.handler(event, context, callback);
    

        2
  •  1
  •   Jaydip Jadhav    7 年前

    您需要定义或导出处理程序函数。

    exports.handler = (username, password) => {
        ...
    }
    
        3
  •  1
  •   IftekharDani    5 年前

    //index.js
    
    const user = require('./user').user;
    const handler = function (event, context, callback) {
      user.login(username, password)
        .then((success) => {
          //success
        })
        .catch(() => {
          //error
        });
    };
    
    exports.handler = handler;
    
    
    
    //user.js
    const user = {
      login(username, password) {
       return new BPromise((resolve, reject) => {
         //do what you want.
       });
      }
    };
    export {user};
    
        4
  •  -1
  •   Eric Wiener    5 年前

    如果您正试图使用带有Amazon技能的typescript并遇到此问题,那么很可能您的事件处理程序正在编译到子文件夹中,而不是主目录中。

    默认情况下,AWS会在事件的根目录中查找事件处理程序 lambda 文件夹。但是,如果您使用typescript并将输出编译为 build 文件夹中,您需要更改AWS查找处理程序的位置。修改 ask-resources.json 在技能的根目录中:

    // ...
    "skillInfrastructure": {
        "userConfig": {
            "runtime": "nodejs10.x",
            "handler": "index.handler",
            "awsRegion": "us-east-1"
        },
        "type": "@ask-cli/lambda-deployer"
    }
    // ...
    

    // ...
    "skillInfrastructure": {
        "userConfig": {
            "runtime": "nodejs10.x",
            "handler": "build/index.handler",
            "awsRegion": "us-east-1"
        },
        "type": "@ask-cli/lambda-deployer"
    }
    // ...
    
    推荐文章