【问题标题】:save response of http request on variable and extract it from the function on node.js将 http 请求的响应保存在变量上并从 node.js 上的函数中提取
【发布时间】: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


    【解决方案1】:

    擦除 res.send(body),并将响应放入 process.stdout.write(d) 但名称不同的函数中(不是 res,做响应),并在开始时更改名称路径。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-26
      • 1970-01-01
      • 2020-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-14
      相关资源
      最近更新 更多