我有一个函数并在app.get()中调用这些函数

编程入门 行业动态 更新时间:2024-10-03 08:32:17

<a href=https://www.elefans.com/category/jswz/34/1766430.html style=我有一个函数并在app.get()中调用这些函数"/>

我有一个函数并在app.get()中调用这些函数

我在 app.get() 中调用 getpage() 并尝试在发送响应时获取所有信息

  1. 数据:

  2. 成功:

  3. 状态:

  4. 留言:

    但是我只收到成功、状态、消息,但没有收到数据

   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()中调用这些函数

本文发布于:2024-05-31 06:31:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1771370.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:我有   并在   函数   一个函数   app

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!