在同一个 EJS 文件中调用另一个函数

Call another function within same EJS file

我试图调用同一个 ejs 文件中的另一个函数。

async function (request, state, logger) {
    var fs = require('fs');
    var path = require('path');
    var util = require('util');
    var response;
    var RspFilename = "my-success.xml";
    var responsepath = path.join('ConfigFiles', 'Responses', 'ABS', RspFilename);
    response = fs.readFileSync((responsepath), 'utf8');

    await this.sleep(1000);
    return {
        headers: {
            "HTTP-Response-Code": 200,
            "HTTP-Response-Code-Text": "OK",
            "Content-Type": "text/xml"
        },
        "statusCode": "200",
        body: response
    };
}


function sleep(ms) {
    return new Promise((resolve) => {
        setTimeout(resolve, ms);
    });
} 

我正在尝试这个江湖骗子。调用端点时,我得到以下错误

{
    "errors": [
        {
            "code": "invalid injection",
            "message": "invalid response injection",
            "source": "(async function (request, state, logger) {\n    var fs = require('fs');\n    var path = require('path');\n    var util = require('util');\n    var response;\n    var RspFilename = \"my-success.xml\";\n    var responsepath = path.join('ConfigFiles', 'Responses', 'ABS', RspFilename);\n    response = fs.readFileSync((responsepath), 'utf8');\n\n    logger.info(\"---------Response Sent Successfully--------\" + RspFilename + response);\n    await this.sleep(1000);\n    return {\n        headers: {\n            \"HTTP-Response-Code\": 200,\n            \"HTTP-Response-Code-Text\": \"OK\",\n            \"Content-Type\": \"text/xml\"\n        },\n        \"statusCode\": \"200\",\n        body: response\n    };\n}\n\n\nfunction sleep(ms) {\n    return new Promise((resolve) => {\n        setTimeout(resolve, ms);\n    });\n})(scope, injectState, logger, deferred.resolve, imposterState);",
            "data": "Unexpected token function",
            "name": "Error",
            "stack": "Error\n    at Object.from (C:\Repo\mountebank\src\util\inherit.js:15:17)\n    at createError (C:\Repo\mountebank\src\util\errors.js:10:26)\n    at Object.InjectionError (C:\Repo\mountebank\src\util\errors.js:25:16)\n    at inject (C:\Repo\mountebank\src\models\responseResolver.js:43:44)\n    at processResponse (C:\Repo\mountebank\src\models\responseResolver.js:243:20)\n    at Object.resolve (C:\Repo\mountebank\src\models\responseResolver.js:277:16)\n    at Object.resolve (C:\Repo\mountebank\src\models\stubRepository.js:136:25)\n    at F.respond (C:\Repo\mountebank\src\models\http\baseHttpServer.js:94:34)\n    at C:\Repo\mountebank\src\models\abstractServer.js:105:39\n    at _fulfilled (C:\Repo\mountebank\node_modules\q\q.js:854:54)"
        }
    ]
}

实际上我的要求是为响应添加一些延迟。

顺便说一句,我试过 Mountebank wait,但看不到任何延迟。

您正在声明一个函数,但忘记在第 1 行为其命名。

如果您打算定义一个匿名函数而不是创建一个 async function 语句,您需要将其括在括号中。

根据 http://www.mbtest.org/docs/api/injection 上的代码示例,似乎 mountebank 不希望任何东西跟随单个匿名函数,因此当它开始读取 function sleep 时出现“意外的标记函数”。

尝试将 sleep 移动到另一个函数中。不远了,因为你已经对模块导入做了同样的事情:

async function (request, state, logger) {
    // ...
    function sleep(ms) {
        return new Promise((resolve) => {
            setTimeout(resolve, ms);
        });
    } 

    await sleep(1000);
    // ...
}

或者干脆

async function (request, state, logger) {
    // ...
    await new Promise(resolve => setTimeout(resolve, 1000));
    // ...
}

或者,简化整个函数

function (request, state, logger) {
    // ...
    return new Promise(resolve => setTimeout(() => {
        resolve({
            headers: // ...

        })
    }, 1000);
}