javascript - 在 HTTP 触发器 azure 中等待 Newman 响应

标签 javascript node.js azure postman newman

概述: 向 node.js azure 函数发送请求。在它内部,有一个 Newman 模块来运行收集请求。 Newman 返回事件发射器,我应该监听它,这意味着它异步运行。

目标: 因为我是js和node js的新手。如何从 Newman 返回响应,然后将其作为 HTTP 请求的响应正文返回我知道,await 不会等待(on)监听,因为它是异步运行的

我的解决方案 我将 Newman 的回复写在一个文件中。并向 azure 函数发出另一个请求以获得响应。还有比这更好的吗?

const newman = require('newman'); // require newman in your project

// call newman.run to pass `options` object and wait for callback
async function run() {
    await newman.run({
        collection: require('./weather.postman_collection.json'),
        reporters: 'cli'
    }, function (err) {
        if (err) { throw err; }
        console.log('collection run complete!');
    }).on('request', function (error, args) {
        if (error) {
            console.error(error);
        }
        else {
            // Log the response body

            console.log(args.response.stream.toString());
            return args.response.stream.toString()
        }
    });

}

module.exports = {
    run
}

const pip = require('./pipline')

module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');

    const name = (req.query.appName || (req.body && req.body.name));
    const responseMessage = name
        ? "Hello, " + name + ". This HTTP triggered function executed successfully."
        : "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.";
    const result = await pip.run(context)
    
    context.res = {
        // status: 200, /* Defaults to 200 */
        body: args.response.stream.toString()
    };
}

最佳答案

从文件中读取数据并不是一个坏主意。如果你同意的话。但这里是当我们调用 newman 请求时获取所有数据的替代解决方案。

此脚本使用 Newman .on('request') 事件来提取该信息。或者您可以使用 .on('beforeDone')如果您想要所有响应主体,您可能需要稍微修改一下,并且可能使用 appendFileSync 来捕获所有 集合中请求的响应。

我修改了你的代码,请查看

const newman = require('newman'); // require newman in your project

// call newman.run to pass `options` object and wait for callback

async function run() {
    await newman.run({
        collection: require('./weather.postman_collection.json'),
        reporters: 'cli'
        }, function (err) {
            if (err) { throw err; }
            console.log('collection run complete!');
        }).on('request', function (error, args) {
            if (error) {
            console.error(error);
            }
            else {
            // Log the response body
                    console.log(args.response.stream.toString());
            // return args.response.stream.toString()
                    Cosnt data = JSON.parse(args.response.stream.toString());
                    return data;
            }
        });
    }
    module.exports = {
        run
}

在初始调用之后,仅读取了 header 。因此,要将正文解析为 JSON,首先必须从传入流中读取正文数据。而且,由于从 TCP 流读取是异步的,因此 .json() 操作最终会异步。

Note: the actual parsing of the JSON itself is not asynchronous. It's just the retrieving of the data from the incoming stream that i asynchronous.

引用Link 1 & Link 2

关于javascript - 在 HTTP 触发器 azure 中等待 Newman 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70360475/

相关文章:

node.js - mongoose findOne 返回找到的第一个对象,即使条件不成立

javascript - 当对象分配给变量时,变量的行为如何?

sql-server - SSMS 能否显示 Azure Synapse 中的实际执行计划?

javascript - 自定义正则表达式来格式化数字

javascript - Safari、JavaScript 和复制到系统剪贴板

javascript - npm 不安装下划线包

azure - 在 Azure Kubernetes 服务上安装 NGINX Controller

javascript - 在 Javascript 和服务器端语言之间表示数据

node.js - Node PDFKit 管道到多个目标

powershell - 缺少 Microsoft Graph ServicePrincipal