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