[英]How to invoke a function in index.js written in Node.js
I'm new to NodeJS and this might be a very basic question but I would appreciate if you can assist me here.我是 NodeJS 的新手,这可能是一个非常基本的问题,但如果你能在这里为我提供帮助,我将不胜感激。
Following is my index.js file:-以下是我的 index.js 文件:-
'use-strict';
const { myauth } = require('./src/authorizer');
let response;
exports.handler = (event, context, callback) => {
logger.info('hello....function invoked');
try {
logger.info(event);
const resp = myauth(event, context, callback, logger);
response = {
'statusCode': 200,
'body': JSON.stringify({
data: resp
})
}
} catch (err) {
logger.error('handler func', err);
response = {
'statusCode': 500,
'body': JSON.stringify({
message: err,
data: false
})
}
}
return response;
}
Package.json:- Package.json:-
{
"name": "authorizer",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"@types/node": "^13.13.5"
},
"devDependencies": {
"chai": "^4.2.0",
"mocha": "^6.1.4"
}
}
When I execute the command like npm start , it does not invoke the logic written inside function in Index.js file.当我执行npm start之类的命令时,它不会调用 Index.js 文件中 function 中编写的逻辑。
Please let me know how to execute this function.请让我知道如何执行此 function。 Thanks
谢谢
im not sure what you mean with 'invoke', i presume you want to assign the returned function result at your variable.我不确定“调用”是什么意思,我想你想在你的变量中分配返回的 function 结果。 To do this, you can auto-invoke your function with this javascript sintax.
为此,您可以使用此 javascript sintax 自动调用您的 function。
exports.handler = ((event, context, callback) => {
logger.info('hello....function invoked');
try {
logger.info(event);
const resp = myauth(event, context, callback, logger);
response = {
'statusCode': 200,
'body': JSON.stringify({
data: resp
})
}
} catch (err) {
logger.error('handler func', err);
response = {
'statusCode': 500,
'body': JSON.stringify({
message: err,
data: false
})
}
}
return response;
})();
let me know.让我知道。 Thanks!
谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.