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

如何使用aws lambda导入函数?

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

    我正在使用 api gateway 把邮寄请求打到我的邮箱里 lambda 检查 httpMethod is post

    index.js 但我想如果我用的是同样的 λ ,我也可以检查它是否是 get httpMethod ,如果是,则执行以下操作。但我想分开我的密码。我也看到了 λ post.js

    不知何故,它没有传递值或调用中的导出函数 邮政.js 不过。

    索引.js

      const postHandler = require('./post.js');
    
      exports.handler = async (event, context) => {
          try {
              const httpm = event.context["http-method"];
              const rbody = event["body-json"];
    
              console.log(postHandler, 'post handler function?');  // { postHandler: [AsyncFunction] } 'post handler function?'
              console.log(httpm, 'httpmhttpm');  // 'POST'
    
              if (httpm === 'POST') return postHandler(rbody);
          } catch (e) {
              return e;
          }
    
      };
    

    邮政.js

    exports.postHandler = async (rbody) => {
        console.log('I am inside postHandler()');
        console.log(rbody);
    
        return {status: true};
    };
    

    提前感谢您的建议/帮助。

    1 回复  |  直到 7 年前
        1
  •  3
  •   AlexOwl    7 年前

    // default export (change post.js file)
    module.exports = async (rbody) => {
        console.log('I am inside postHandler()');
        console.log(rbody);
    
        return {status: true};
    };
    
    // OR !
    
    // change (index.js file)
    const { postHandler } = require('./post.js');