如何在Sails js中使用外部rest api(nodejs MVC) [英] How to use external rest api in Sails js (nodejs MVC)
问题描述
我使用sailsjs作为节点js的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.
但现在我需要/想要从外部rest api获取数据。
我在我的控制器中使用了这个:
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();
}
变量 d
正在显示正确的结果。
如何在我的视图中使用请求结果?
我搜索了很多,但我无法找到任何方法在我看来显示这个。
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.
这是实时更新的吗?因此,如果de rest api中的内容发生变化,我将无需刷新。
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.
推荐答案
基本上你会等待你的请求触发其回调,然后将获取的数据提供给 res.locals
。假设你正在获取JSON数据,你可以这样做:
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();
}
您提供的示例代码存在一些问题:
-
test:function(res,req)...
不要混淆控制器参数,第一个是_req_uest,第二个是_res_ponse。
test: function(res,req) ...
Don't mixup the controller arguments, the first is _req_uest, the second one _res_ponse.
var req = https。请求...
您真的不想覆盖传递给控制器操作的 req
参数。使用其他名称。
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){...}
同样在这里。对于 https.request
回调范围,这样做会覆盖 res
- 阻止你使用所有的东西(例如: res.view
)由传递给控制器的 res
参数提供。
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:
这篇关于如何在Sails js中使用外部rest api(nodejs MVC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!