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.
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: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 thereq
argument passed into your controller action. Use some other name.https.request(options, function(res) {...}
Same here. Doing this overridesres
for thehttps.request
callback scope - preventing you from using all the goodies (e.g.:res.view
) supplied by theres
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?