如何在Azure中导出node.js函数?

发布于 2025-02-01 13:02:30 字数 2179 浏览 2 评论 0原文

我具有以下结构,其中有2个Azure函数在Azure函数应用中分别运行:

- main_function_app
  - functions
    - Full_Stack
        - configAPI.json
        - configAPITest.json
        - function.json
        - index.js
    - Web  
        - configAPI.json
        - configAPITest.json
        - function.json
        - index.js
    - host.json
    - local.settings.json
    - package-lock.json
    - package.json
    - proxies.json
    - utils.js

我在index.js.js中具有以下内容,来自full_stack

module.exports = async function (context, req) { 
    let element = req.body;
    const purchase_time = Number(element.purchase_time_);
    const type = "Full Stack";

    if (element.startswith('full')){
        async function getStatusPayload(purchase_time, offset);
    }

    async function getStatusPayload(purchase_time, type){
    if (purchase_time>0){
        context.log("Purchase time successful in " + type);
        }
    }
    context.res = {
       // status: 200, /* Defaults to 200 */
       status: 200
    };
}

我在index.js来自Web

module.exports = async function (context, req) { 
    let element = req.body;
    const purchase_time = Number(element.purchase_time_);
    const type = "Web";

    if (element.startswith('web')){
       async function getStatusPayload(purchase_time, type); 
    }

    async function getStatusPayload(purchase_time, type){
    if (purchase_time>0){
        context.log("Purchase time successful in " + type);
        }
    }
    context.res = {
       // status: 200, /* Defaults to 200 */
       status: 200
    };
}

两个功能几乎相同,我想重构getStatusPayload。我已经在utils.js文件中创建了草稿,但是我无法在完整的堆栈或Web索引中对其进行测试,因为我不知道如何从Azure导入它。另外,我不确定一旦我能够将其导入完整堆栈和Web,代码是否会工作。

// utils.js
exports ={
    getStatusPayload: async function (purchase_time, type){
    if (purchase_time>0){
        context.log("Purchase time successful in " + type);
        }
      }
   }
}
module.exports = exports;

我还围绕context.executionContext.FunctionDirectory抛出当前目录,但不确定这是否对解决方案有帮助。

I have the following structure with 2 azure functions running separately in an azure function app:

- main_function_app
  - functions
    - Full_Stack
        - configAPI.json
        - configAPITest.json
        - function.json
        - index.js
    - Web  
        - configAPI.json
        - configAPITest.json
        - function.json
        - index.js
    - host.json
    - local.settings.json
    - package-lock.json
    - package.json
    - proxies.json
    - utils.js

I have the following content in the index.js from Full_Stack:

module.exports = async function (context, req) { 
    let element = req.body;
    const purchase_time = Number(element.purchase_time_);
    const type = "Full Stack";

    if (element.startswith('full')){
        async function getStatusPayload(purchase_time, offset);
    }

    async function getStatusPayload(purchase_time, type){
    if (purchase_time>0){
        context.log("Purchase time successful in " + type);
        }
    }
    context.res = {
       // status: 200, /* Defaults to 200 */
       status: 200
    };
}

I have the following content in the index.js from Web:

module.exports = async function (context, req) { 
    let element = req.body;
    const purchase_time = Number(element.purchase_time_);
    const type = "Web";

    if (element.startswith('web')){
       async function getStatusPayload(purchase_time, type); 
    }

    async function getStatusPayload(purchase_time, type){
    if (purchase_time>0){
        context.log("Purchase time successful in " + type);
        }
    }
    context.res = {
       // status: 200, /* Defaults to 200 */
       status: 200
    };
}

Both functions are almost the same and I want to refactor the getStatusPayload. I have created a draft in the utils.js file but I cannot test it in the either the full stack or web index.js because I don't know how to import it from azure. Also, I am not totally sure if code will even work once I am able to import it to full stack and web.

// utils.js
exports ={
    getStatusPayload: async function (purchase_time, type){
    if (purchase_time>0){
        context.log("Purchase time successful in " + type);
        }
      }
   }
}
module.exports = exports;

I also came around context.executionContext.functionDirectory which throws the current directory, but not sure if this might be helpful in in the solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

私藏温柔 2025-02-08 13:02:30

在您的问题上:要求是我们可以用来导入其他模块的功能:

// it looks like this
let util = require('./util.js');

并导出:

function getStatusPayload() {
    // Code here
}

module.exports = {
    getStatusPayload
}

To your question: require is a function we can use to import other modules:

// it looks like this
let util = require('./util.js');

and to export:

function getStatusPayload() {
    // Code here
}

module.exports = {
    getStatusPayload
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文