javascript - Express 和 Nodejs : Best way to call an external API

标签 javascript jquery node.js express

我是 Express 和 Nodejs 的新手。我正在尝试调用外部 api 来在页面上构建数据。有没有办法从 express 本身调用外部 api(我知道我可以使用 http moduel 来做到这一点)但想确认这是最好的方法。另外,一旦我取回 json,我该如何将其传递给 View 。

现在我已经通过仅使用 express 加载 View (仅 header )并对 api 进行 jquery ajax 调用并填充数据来完成解决方法。

最佳答案

首先,您需要在要填充它的 Controller 内部调用外部 API。最好是使用http 模块,它很容易使用http://nodejs.org/docs/v0.4.10/api/http.html#http.get .获取数据后,只需将其传递给 View 即可,如下所示:

http.get(options, function(data) {
    res.render('template', data);
});

关于javascript - Express 和 Nodejs : Best way to call an external API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25329574/

相关文章:

javascript - 如何让 Materialise 使用下拉列表类而不是 ID?

javascript - Meteor 将输入存储在集合中

jquery - Owl Carousel : not pulling data-src value as background image in CSS for Vimeo videos

node.js - E : Unable to locate package npm

javascript - 带有javascript全局变量的Phonegap

javascript - 如何使用location.hash平滑滚动?

javascript - 如何使用 HERE Maps API 解决车辆路径问题?

jquery - CSS-Jquery : Nested <ul> with different CSS possible?

Node.js:如何向使用自签名证书的应用程序发送 HTTPS 请求

node.js - 如何将本地模块指定为 npm 包依赖项