我有一个函数并在app.get()中调用这些函数"/>
我有一个函数并在app.get()中调用这些函数
我在 app.get() 中调用 getpage() 并尝试在发送响应时获取所有信息
数据:
成功:
状态:
留言:
但是我只收到成功、状态、消息,但没有收到数据
function getpage(url, cks) {
const curl = new Curl()
const urld = url
console.log(urld);
curl.setOpt(Curl.option.URL, urld)
curl.setOpt(Curl.option.SSL_VERIFYPEER, 0)
var gh = curl.setOpt(Curl.option.COOKIEFILE, 'cookies/' + cks)
curl.setOpt(Curl.option.FOLLOWLOCATION, true)
console.log(gh)
curl.on('end', function (statusCode, body, headers) {
var result = body;
console.log(result);
return result;
this.close();
});
curl.on('error', function (err, curlErrorCode) {
console.error(err);
console.error(curlErrorCode);
this.close();
});
const hg = curl.perform();
}
app.get('/all', (req, res) => {
let geturl = req.query.url;
let getemail = req.query.email;
res.json({
data: getpage(geturl, getemail),
success: true,
status: 200,
message: "Done"
})
})
回答如下:
您正在使用 node-libcurl 发出请求,但没有对该请求中的数据执行任何操作。
getpage()
的结果是未定义的,该函数永远不会返回任何内容。修改 getpage 以接受回调,并使用它从 'end'
事件侦听器中获取数据。 (或者查看Fetch
,很方便)。
function getpage(url, cks, cb) {
const curl = new Curl()
const urld = url
curl.setOpt(Curl.option.URL, urld)
curl.setOpt(Curl.option.SSL_VERIFYPEER, 0)
curl.setOpt(Curl.option.COOKIEFILE, 'cookies/' + cks)
curl.setOpt(Curl.option.FOLLOWLOCATION, true)
curl.on('end', function (statusCode, body, headers) {
var result = body;
// use your callback here!
cb(result);
this.close();
});
curl.on('error', function (err, curlErrorCode) {
console.error(err);
console.error(curlErrorCode);
this.close();
});
curl.perform();
}
app.get('/all', (req, res) => {
let geturl = req.query.url;
let getemail = req.query.email;
// call getpage and pass in a function
// expecting the results
getpage(geturl, getemail, (results) => {
res.json({
data: results,
success: true,
status: 200,
message: "Done"
})
})
})
更多推荐
我有一个函数并在app.get()中调用这些函数
发布评论