AWS就是这么说的:
函数中的模块名.导出值。例如,"index.handler“在index.js中调用exports.handler。
它正确地调用了这个函数:
exports.handler = (username, password) => {
...
}但是如果代码是这样的呢:
module.exports = (username, password) => {
...
}我怎么称呼它?我没有尝试过像module.exports、module.handler等作品。
发布于 2018-10-04 19:49:05
AWS Lambda期望您的模块导出包含处理程序函数的对象。然后在Lambda配置中声明包含模块的文件和处理程序函数的名称。
在Node.js中导出模块的方式是通过module.exports属性进行的。require调用的返回值是文件计算结束时module.exports属性的内容。
exports只是指向module.exports的局部变量。您应该避免使用exports,而应该使用module.exports,因为其他代码可能会覆盖module.exports,从而导致意外行为。
在第一个代码示例中,模块使用单个函数handler正确地导出对象。但是,在第二个代码示例中,您的代码导出一个函数。因为这与AWS Lambda的API不匹配,所以这是行不通的。
考虑以下两个文件,export_object.js和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')时,我们得到一个具有单个函数的对象:
> 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配置为运行一个名为handler的函数(该函数是在文件index.js中定义的模块中导出的)时,下面是亚马逊在调用函数时所做的一个近似:
const handler_module = require('index.js');
return handler_module.handler(event, context, callback);重要的部分是调用模块中定义的处理程序函数。
发布于 2018-10-04 05:03:45
您需要定义或导出处理程序函数。
exports.handler = (username, password) => {
...
}发布于 2018-10-04 05:26:57
我像这样用过。
//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};https://stackoverflow.com/questions/52639302
复制相似问题