简体   繁体   中英

Waiting for newman response in HTTP Trigger azure

Overview: sending a request to node.js azure function. inside it, there is a Newman module to run a collection request. Newman returns event emitter which I should listen to it which means it runs asynchronously.

Goal: Because I am new in js and node js. How can I return the response from Newman and then returns it as the response body of the HTTP request I know that await will not wait for (on) listening because it runs asynchronously

MySolution That I write the response from Newman in a file. and make another request to azure function to get the response.? there is better than that?

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()
    };
}

Reading data from file is not a bad idea. If you are Okay with it. But here is the alternate solution to get all data when we call newman request.

This script is using the Newman .on('request') event, that will extract that information. Or you can use .on('beforeDone') If you wanted all the response bodies you may need to modify this slightly and maybe use the appendFileSync to capture all the responses from the requests in the collection.

I have modified your code please have a look

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
}

After the initial call only the headers have been read. So, to parse the body as JSON, first the body data has to be read from the incoming stream. And, since reading from the TCP stream is asynchronous, the.json() operation ends up asynchronous.

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.

Refer Link 1 & Link 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM