just need to console.log output in a variable (let body on my code) rather than in the console, and res.send (“body” not “data” that send now) this variable on angular/postman where make the request as a response.
app.post('/newreq/:uni', (req, res) => { pool.getConnection((err, connection) => { if(err) throw err connection.query('SELECT conn_id,cred_def_id FROM uniConn WHERE uni = ?', [req.params.uni], (err, result, fields) => { connection.release() // return the connection to pool let connection_id =result[0].conn_id; let cred_def_id =result[0].cred_def_id; const http = require('http') const data = JSON.stringify({ "connection_id": `${[connection_id]}`, "new_request": { "requested_attributes": { "0_name_uuid": { "name": "score", "restrictions": [ { "cred_def_id": `${[cred_def_id]}` } ] } }, "requested_predicates": { "0_grade_GE_uuid": { "name": "score", "restrictions": [ { "cred_def_id": `${[cred_def_id]}` } ] } } } }) const options = { hostname: 'localhost', port: 9090, path: '/new-request/send-request', method: 'POST', headers: { 'Content-Type': 'application/json', 'Content-Length': data.length } } let body=""; req = http.request(options, res => { console.log(`statusCode: ${res.statusCode}`) res.on('data', d => { process.stdout.write(d) body+=d; }) }) req.on('error', error => { console.error(error) }) req.write(data) req.end() res.send(data) }) }) });
if i console.log(body: ${body}
) inside the function, give the same result on terminal as process.stdout.write(d). So my problem is how to extract the variable with new value out of the function so can send it as response with res.send(body).
Advertisement
Answer
erase the res.send(body), and put the response inside the function that is process.stdout.write(d) but with different name (not res, do it response), and change the name and at the start of the path.
app.post('/newreq/:uni', (req, response) => { .... .... .... req = http.request(options, res => { console.log(`statusCode: ${res.statusCode}`) res.on('data', d => { process.stdout.write(d) response.send(d) }) }) req.on('error', error => { console.error(error) }) req.write(data) req.end() }