如何从AWS Lambda(Node.js)的处理程序中调用module.exports [英] How to call module.exports from handler in AWS Lambda (Node.js)

查看:136
本文介绍了如何从AWS Lambda(Node.js)的处理程序中调用module.exports的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AWS就是这样:

函数中的module-name.export值.例如, "index.handler"在index.js中调用exports.handler.

The module-name.export value in your function. For example, "index.handler" calls exports.handler in index.js.

它正确调用了此函数:

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

但是如果代码是这样的:

But what if the code is like this:

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

我怎么称呼它?我没有尝试过像module.exportsmodule.handler等这样的作品.

How do I call it? Nothing I tried works like module.exports, module.handler, etc.

推荐答案

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

AWS Lambda expects your module to export an object that contains a handler function. In your Lambda configuration you then declare the file that contains the module, and the name of the handler function.

在Node.js中导出模块的方式是通过module.exports属性. require调用的返回值是文件评估结束时module.exports属性的内容.

The way modules are exported in Node.js is via the module.exports property. The return value of a require call is the contents of the module.exports property at the end of the file evaluation.

exports只是指向module.exports的局部变量.您应该避免使用exports,而应使用module.exports,因为其他一些代码可能会覆盖module.exports,从而导致意外的行为.

exports is just a local variable pointing to module.exports. You should avoid using exports, and instead use module.exports, since some other piece of code may overwrite module.exports, leading to unexpected behaviour.

在您的第一个代码示例中,模块使用单个功能handler正确导出对象.但是,在第二个代码示例中,您的代码导出了一个函数.由于这与AWS Lambda的API不匹配,因此无法使用.

In your first code example, the module correctly exports an object with a single function handler. In the second code example, however, your code exports a single function. Since this does not match AWS Lambda's API, this does not work.

请考虑以下两个文件export_object.js和export_function.js:

Consider the following two files, export_object.js and export_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')时,我们将获得一个具有单个功能的对象:

When we run require('export_object.js') we get an object with a single function:

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

将其与运行require('export_function.js')时得到的结果进行比较,在此我们仅获得一个函数:

Compare that with the result we get when we run require('export_function.js'), where we just get a function:

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

当您配置AWS Lambda运行名为handler的功能时,该功能在文件index.js中定义的模块中导出,这是亚马逊在调用该功能时的近似方法:

When you configure AWS Lambda to run a function called handler, that is exported in a module defined in the file index.js, here is an approximation of Amazon does when a function is called:

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

最重要的部分是对模块中定义的处理程序函数的调用.

The important part there is the call to the handler function defined in the module.

这篇关于如何从AWS Lambda(Node.js)的处理程序中调用module.exports的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆