Reputation: 6970
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.
Upvotes: 4
Views: 9214
Reputation: 867
// 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');
Upvotes: 4