Node.js 如何获取http.get本地主机
我正在尝试从端口1717获取页面,但当我的带宽不可用时,http.get on错误回调会记录errno enoint,而当我重新打开带宽时,它会记录errno econreset。无论我的计算机是否处于脱机状态,只要服务器启动并运行,url都会返回浏览器中的内容。我尝试过使用postman,也尝试过使用http模块的Node.js 如何获取http.get本地主机,node.js,request,localhost,Node.js,Request,Localhost,我正在尝试从端口1717获取页面,但当我的带宽不可用时,http.get on错误回调会记录errno enoint,而当我重新打开带宽时,它会记录errno econreset。无论我的计算机是否处于脱机状态,只要服务器启动并运行,url都会返回浏览器中的内容。我尝试过使用postman,也尝试过使用http模块的request方法,而不是get。我ended请求在使用它之后,但仍然得到相同的错误 同时,我尝试了获取除localhost上的链接之外的其他链接,它获取了它们。在其他一些线程中,我
request
方法,而不是get
。我end
ed请求在使用它之后,但仍然得到相同的错误
同时,我尝试了获取除localhost上的链接之外的其他链接,它获取了它们。在其他一些线程中,我看到有人建议我使用hostname 127.0.0.1而不是localhost。这也不起作用,所以我从npm切换到请求模块,然后在行为上有一点不同。在server.js中,我使用了类似这样的方法来获取命中/admin/available的请求/
console.log('giving you table', table)
res.writeHead(200, {"Content-Type": "text/html"});
res.end(table);
但是,当我使用请求模块时,它会抛出错误EconReset,但在服务器运行的CLI窗口中,会记录此console.log('giving you table',table)
,这意味着服务器确实看到该请求,但不知何故,模块仍会抛出EconReset,并且未定义body和response变量,声称它看不见它。我能做些什么?
我将在下面发布我的代码,以防我遗漏了什么
var http = require('http'),
component = require('../lib/render-component'),
render = {username: '', available: '', orders: '', frequent: ''};
// for simplicity
var request = require('request');
request('http://localhost:1717/admin/available', function(err, res, body) {
console.log(err, res, body)
});
// intended use scenario
http.get({port: 1717, path: '/admin/available/', headers: {Accept: 'text/html'}}, function(res) {
var temp = '';
res.setEncoding('utf8');
console.log('inside get');
res.on('data', function (chunk) {
temp += chunk;
}).on('end', function() {
render.available = temp;
http.get('http://localhost:1717/admin/order/?page=0', function(res) {
var temp = ''
res.setEncoding('utf8');
res.on('data', function (chunk) {
temp += chunk;
}).on('end', function() {
render.orders = temp;
ordersModel.find({status: 'delivered'}, 'food', function (err, orders) {
if (err) throw err;
var hashMap = [], returnArr = [];
orders.forEach(function (order) {
hashMap.push(order.toObject()['food'].split(","));
})
hashMap.reduce(function(a, b) {
return a.concat(b)
}, []).forEach(function(item) {
if ((k = returnArr.findIndex(function(elem) {
return elem[0] == item;
})) != -1) {
returnArr[k][1]++;
}
else returnArr.push([item, 1]);
})
// filter the ones with the highest value
hashMap = [], returnArr = returnArr.sort(function(a, b) {
return b[1] - a[1];
}).slice(0, 5).forEach(function(elem) {
hashMap.push({name: elem[0], counter: elem[1]})
});
render.frequent = component("frequent", hashMap);
console.log(render)
}) // orders model find
}); // get orders
}).on('error', function(e) {
console.log(e)
});
}); // available on end
}).on('error', function(e) {
console.log('err available', e)
}); // available on error
请帮帮我。我已经被困了三天了。难道你不应该尝试像服务器端的express和在服务器api之间发送http请求的request这样的方法吗?这段代码看起来很糟糕,实现了一些可以通过承诺或使用orm来完成的事情。尝试产生更多的输出。@Mr.Phoenix问题是否与代码组织有关?如果没有获取任何资源,Promissions仍将接收错误对象。此外,这是一个试图批量获取一系列资源的代理,而不是实际的服务器本身。我还需要快递吗?