It looks like you already have the result inside your nc.request
callback, so you can likely send your response from inside that callback.
app.post('/sum',(req,res)=> {
nc.request('my.request', {x: 2, y:3 }, {max: 1}, resp => {
console.log("Response from subscriber: " + resp)
// Send here, since you have the response
res.status(200).send(resp)
})
})
However, if nc.request
returns a Promise containing the response data, you can await it inside an async function.
app.post('/sum', async (req,res, next)=> {
try {
const result = await nc.request('my.request', {x: 2, y:3 }, {max: 1})
console.log("Response from subscriber: " + result)
res.status(200).send(result)
}
catch (err) {
// Have express handle errors from the async function
next(err)
}
})