1

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.

3
  • Azure Functions JavaScript developer guide tried? Commented May 26, 2022 at 6:19
  • It seems I need to add a script file. I have applied the changes but how should I write the utils.js file? I don't think my syntax is correct. Commented May 26, 2022 at 6:24
  • Bit too long for comment. I answered normally. Commented May 26, 2022 at 17:00

1 Answer 1

1

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
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.