在Controller - sails.js中创建一个HTTP请求
Make a HTTP request in your Controller - sails.js
是否可以在控制器中发出请求?我试过使用node.js http模块,但没有任何成功。还有其他方法吗?
好的,我设法解决了这个使用另一个模块'请求'。我做了什么:
在项目中安装模块:
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);
}
});
对于来自未来的人,请求包已从2020年2月11日起完全弃用。
请求所推荐的备选选项如下:
+-------------------+-------------+-----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 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. |
+-------------------+-------------+-----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
我包装节点的原生https。Get(或http.get)在promise中,然后像这样从控制器调用:
const https = require('https')
// wrap node's https.get stream call as a promise
// note: assumes utf-8 encoded data payload to get.
async function getdata(url) {
return new Promise((resolve, reject) => {
https.get(url, (res) => {
res.setEncoding('utf8');
let data = '';
res.on('data', (chunk) => {
data = data + chunk;
});
res.on('end', () => {
resolve(data);
})
}).on('error', (e) => {
reject(e);
});
});
}
// Call from sails controller
module.exports = {
myaction: async function(req, res) {
let data;
try {
data = await getdata('https://example.com/index.html');
} catch (e) {
// handle it
}
...
}
}
相关文章:
- Wicket在另一个请求之后触发请求
- 如何使请求等待上一个请求完成
- Nodejs一个请求阻塞另一个请求
- 如何在调用下一个请求之前完成第一个Ajax Get请求
- 如何将请求响应传递给另一个请求
- 主干-用一个请求替换整个集合
- Node.js在一个请求中提供多个文件
- 在angular cordova应用程序中发布一个请求中的多个文件
- $http请求在一个请求中返回所有记录而不分页
- 有没有一种方法可以用一个请求加载多个iframe
- 如果用户发送另一个请求,请使用 .abort() 取消现有的 ajax 请求
- 如何使用 angularjs 将$http请求返回的数据用于同一控制器中的另一个请求
- Ajax 触发两次替换 Rails 4 中的最后一个请求内容
- 在完成上一个请求之前中止新的 AJAX 请求
- 如何阻止 ajax 重复上一个请求
- Rails:在一个请求中渲染 html 和 js
- Javascript 一个请求多个 JS 文件
- JavaScript在一个请求中处理不同的内容类型
- MongoDB-组合多个命令来发送和返回一个请求
- Instagram API没有在一个请求中提取所有数据