是否可以在控制器中发出请求?我尝试过使用node.js http模块,但没有任何成功。有没有其他方法可以做到这一点?
答案 0 :(得分:13)
好的,我设法使用另一个模块'请求'来解决这个问题。我做了什么:
在项目中安装模块:
npm install -S request
在您的代码中,您应该:
var request = require('request');
request.get({
url: <your url>
}, function(error, response, body) {
if (error) {
sails.log.error(error);
}
else {
sails.log.info(response);
sails.log.info(body);
}
});
&#13;
答案 1 :(得分:2)
对于未来的人,request package自2020年2月11日起已完全弃用。
请求具有recommended的替代选项如下所示。
+-------------------+-------------+-----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Package Name | Bundle Size | API Style | Summary |
+-------------------+-------------+-----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| node-fetch | 0.4kb | promise / stream | A light-weight module that brings window.fetch to Node.js |
+-------------------+-------------+-----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| bent | 1kb | fp / promise / stream | Functional HTTP client w/ async/await |
+-------------------+-------------+-----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| got | 48.4kb | promise / stream | Simplified HTTP requests |
+-------------------+-------------+-----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| make-fetch-happen | 442kb | promise / stream | make-fetch-happen is a Node.js library that wraps node-fetch-npm with additional features node-fetch doesn’t intend to include, including HTTP Cache support, request pooling, proxies, retries, and more! |
+-------------------+-------------+-----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| axios | 11.9kb | promise / stream | Promise based HTTP client for the browser and node.js |
+-------------------+-------------+-----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| unfetch | 1kb | promise / stream | Tiny 500b fetch “barely-polyfill” |
+-------------------+-------------+-----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| superagent | 18kb | chaining / promise | Small progressive client-side HTTP request library, and Node.js module with the same API, sporting many high-level HTTP client features |
+-------------------+-------------+-----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tiny-json-http | 22kb | promise | Minimalist HTTP client for GET and POSTing JSON payloads |
+-------------------+-------------+-----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| needle | 164kb | chaining / promise | The leanest and most handsome HTTP client in the Nodelands |
+-------------------+-------------+-----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| urllib | 816kb | callback / promise | Help in opening URLs (mostly HTTP) in a complex world — basic and digest authentication, redirections, cookies and more. |
+-------------------+-------------+-----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
答案 2 :(得分:1)
我将节点的本机https.get(或http.get)包装在Promise中,然后像这样从控制器调用:
[options]