admin管理员组文章数量:1391955
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.
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.
Share Improve this question asked Mar 13, 2019 at 21:55 DoraDora 7,01015 gold badges58 silver badges116 bronze badges 4- excuse me, yeah. The problem is the wrong import – AlexOwl Commented Mar 13, 2019 at 22:12
-
const { postHandler } = require('./post.js');
– AlexOwl Commented Mar 13, 2019 at 22:12 - @AlexOwl oh my my~ how can I miss that darn! – Dora Commented Mar 13, 2019 at 22:13
- 1 we are so tired – AlexOwl Commented Mar 13, 2019 at 22:15
1 Answer
Reset to default 4
// 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');
本文标签: javascripthow to import functions with aws lambdaStack Overflow
版权声明:本文标题:javascript - how to import functions with aws lambda? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744739092a2622504.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论