How to call module.exports from handler in AWS Lambda (Node.js)

本秂侑毒 提交于 2020-01-23 13:51:10

问题


That's what says in AWS:

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

And it correctly calls this function:

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

But what if the code is like this:

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

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


回答1:


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.

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 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.

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.

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;

and

// export_function.js

function internal_foo () {
    return 1;
}

module.exports = internal_foo;

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] }

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]

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.




回答2:


You need to define or export your handler function.

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



回答3:


In my aws lamda i have use like this.

//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/how-to-call-module-exports-from-handler-in-aws-lambda-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!