Search code examples
javascriptnode.jsrestreal-timesails.js

How to use external rest api in Sails js (nodejs MVC)


Im using sailsjs as a MVC for node js, i'm still learning it. I managed to get data from my own database and use it.

But now i need/want to get data from an external rest api. I used this in my controller:

// api/controllers/SomeController.js
test : function(res,req){
    var j;

    var https = require('https');

    var options = {
      hostname: 'testing.atlassian.net',
      port: 443,
      path: '/rest/api/2/search?jql=project=ABC',
      method: 'GET',
      headers: {'Authorization': 'Basic ' + 'SuperSecretLoginAndPassword'}
    };

    var req = https.request(options, function(res) {
      res.setEncoding('utf8');
      res.on('data', function(d) {

      });
     });

    req.end();
}

The variable d is displaying the right result. How can i use the request results in my view? I've searched a lot but i cant find any ways to display this in my view.

And will this be realtime updated? So if something in de rest api changes I won't have to refresh.

Sorry if this is something stupid.


Solution

  • Basically you'll want to wait for your request to fire its callback and then feed the fetched data into res.locals. Assuming you are fetching JSON data, you could do this:

    // api/controllers/SomeController.js
    test: function(req, res) {
      var https = require('https');
    
      ...
    
      https.request(options, function(response) {
        var responseData = '';
        response.setEncoding('utf8');
    
        response.on('data', function(chunk){
          responseData += chunk;
        });
    
        response.once('error', function(err){
          // Some error handling here, e.g.:
          res.serverError(err);
        });
    
        response.on('end', function(){
          try {
            // response available as `responseData` in `yourview`
            res.locals.requestData = JSON.parse(responseData);
          } catch (e) {
            sails.log.warn('Could not parse response from options.hostname: ' + e);
          }
    
          res.view('yourview');
        });
      }).end();
    
    }
    

    The example code you supplied has some issues:

    • test: function(res,req) ... Don't mixup the controller arguments, the first is _req_uest, the second one _res_ponse.

    • var req = https.request ... You really do not want to override the req argument passed into your controller action. Use some other name.

    • https.request(options, function(res) {...} Same here. Doing this overrides res for the https.request callback scope - preventing you from using all the goodies (e.g.: res.view) supplied by the res parameter passed to your controller.

    I'd guess it would make sense for you to read up on closures and callbacks:

    What are Closures and Callbacks?