Search code examples
javascriptnode.jsnode-rest-client

Node.js displaying data using node-rest-client from Api


I am new to Node.js and I am stuck with small issue. My api is returning JSon but I am not able to see JSon in data or response.

router.get('/search/:id', function(req, res){client.get("http://localhost:3000/api/search/5600678e1c76b4680e0d6544", function(data, response){
         
        console.log(data);
        console.log(response);

res.render('test', {test: data,  user : req.user , title : 'Home'});
    });
});

Solution

  • EDIT: I understood you wrong here another way of how to do it using the npm module request

    var request = require('request');
    
    router.get('/search/:id', function(req, res) {
      request('http://localhost:3000/api/search/5600678e1c76b4680e0d6544', function(error, response, body) {
        if (!error && response.statusCode == 200) {
          console.log(body)
          var data = body;
          res.render('test', {
            test: data,
            user: req.user,
            title: 'Home'
          });
        } else {
          res.end('Error: ' + error);
        }
      });
    });
    

    If this is not working than maybe the error is on localhost:3000 not returning anything.