简体   繁体   English

如何在用 Node.js 编写的 index.js 中调用 function

[英]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.

相关问题 错误:文件 index.js 定义的 Node.js 模块应该导出名为 xxxx 的函数 - Error: Node.js module defined by file index.js is expected to export function named xxxx 通过节点的 index.js 以编程方式调用黄瓜 cli - Invoke cucumber cli programatically through index.js of node node.js 如何在不指定文件夹路径的情况下使用 index.js 解析所需文件夹? - How node.js resolves required folders using index.js without specifying folder path? 如何将index.js中的json对象发送到node.js中的html - how to send a json object from index.js to html in node.js Node.js,如何访问Index.js中导出的模块? - In Node.js, How can I access exported module in Index.js? 如何在index.js中切换到新功能 - How to switch to a new function in index.js Node.js-如何在函数内调用方法? - Node.js - How can i invoke a method inside a function? 如何让 $node index.js 在我的命令行中运行 index.js? - How can I get $node index.js to run index.js in my command line? 503将node.js部署从server.js切换到index.js(初学者)时出错 - 503 Error when switching node.js deployment from server.js to index.js (beginner) node.js命令行程序(使用Commander节点模块)在执行时打开index.js - node.js command line programs(using commander node-module) opens index.js on execution
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM