5

I am using api gateway to call a post request into my lambda which checks if the httpMethod is post, if post then do the following.

by default the entry point is index.js which I kept but then I am thinking if I am using the same lambda, I can also check if it's a get httpMethod, if so then do the following. But I want to separate my codes. Which I see for the same lambda function, I can add files. So I tried to add another file named post.js then require at index.js

Somehow, it's not passing values or calling the exported function in post.js though.

index.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;
      }

  };

post.js // not doing anything special here, but none of these console shows up

exports.postHandler = async (rbody) => {
    console.log('I am inside postHandler()');
    console.log(rbody);

    return {status: true};
};

Thanks in advance for any suggestions / help.

4
  • excuse me, yeah. The problem is the wrong import Commented Mar 13, 2019 at 22:12
  • const { postHandler } = require('./post.js'); Commented Mar 13, 2019 at 22:12
  • @AlexOwl oh my my~ how can I miss that darn! Commented Mar 13, 2019 at 22:13
  • 1
    we are so tired Commented Mar 13, 2019 at 22:15

1 Answer 1

5

// 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');

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.