【发布时间】:2021-06-18 11:33:24
【问题描述】:
只需要将 console.log 输出到一个变量中(让 body 在我的代码上)而不是在控制台中,然后 res.send(“body”而不是“现在发送的”数据)这个变量在 angular/postman 上 make请求作为响应。
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)
})
})
});
如果我在函数内部使用 console.log(body: ${body}),则在终端上给出与 process.stdout.write(d) 相同的结果。所以我的问题是如何从函数中提取具有新值的变量,以便可以使用 res.send(body) 将其作为响应发送。
【问题讨论】:
标签: javascript node.js http httprequest httpresponse