如何在NodeJs路由中使用请求模块

时间:2017-11-19 20:58:49

标签: node.js express module request httprequest

如何正确使用app.get('/ ax')中的请求模块。我发现只在其他问题中请求模块使用,但如何触发该功能

var request = require("request")

var url = "https://www.lonelyplanet.com/usa/san-francisco/attractions/de-young-museum/a/poi-sig/1340028/1329646";

module.exports = app => {
    app.get('/axe', (req, res, next) => {
req.request({
    url: url,
    json: true
}, function (error, response, body) {

    if (!error && response.statusCode === 200) {
        console.log(body) // Print the json response
    }
})
    });
}

1 个答案:

答案 0 :(得分:1)

简单如下:

  app.get('/axe', (req, res, next) => {
    request.get(url, function (error, response, body) {    
      if (!error && response.statusCode === 200) {
        console.log(body) // Print the json response
      }
    })
 });