我正在使用express.js,我需要调用HTTP GET请求来获取JSON数据。请建议我一些好的node js/express js模块/lib来执行get/post请求。
最佳答案
Node.js 以 http.request
的形式为此功能提供了一个极其简单的 API。
var http = require('http');
//The url we want is: 'www.random.com/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new'
var options = {
host: 'www.random.com',
path: '/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new'
};
callback = function(response) {
var str = '';
//another chunk of data has been recieved, so append it to `str`
response.on('data', function (chunk) {
str += chunk;
});
//the whole response has been recieved, so we just print it out here
response.on('end', function () {
console.log(str);
});
}
http.request(options, callback).end();
Here我附加了一些带有 POST 和自定义 header 的示例。如果您不需要特殊的东西,我会坚持使用 native 代码。
此外,Request , Superagent或Requestify都是非常好的使用库。
关于javascript - Node JS 或 Express JS HTTP GET 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29896322/