所以我试图做的基本上是创建一个服务器来接收请求,发出新请求,接收新响应,然后将其转发给原始源。
这是我的代码:
var http = require('http');
var request = require('request');
var server = http.createServer(function(request, response){
response.writeHead(200, {"Content-Type": "text/plain"});
response.write('on');
getSummonerId(response);
});
server.listen(9000);
console.log("Server running!");
function getSummonerId(cb){
request('https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/' + 'someones-name' + '?api_key=mykeygoeshere', function(request, response, body){
cb(JSON.parse(body));
});
}
当我这样做时,我得到TypeError: Object #<Object> has no method '_implicitHeader'
。
在进一步搜索googel的书后,我发现了这些:
https://github.com/joyent/node/issues/5597
How can I properly implement the nodejs Stream API?
我认为这可能是我遇到的问题,但我在将原始问题的背景转化为问题的背景时遇到了麻烦。
答案 0 :(得分:0)
好吧,我认为这里有多个问题,但最重要的是你将response
对象作为回调传递到getSummonerId()
函数中,但它不是功能,它是一个对象。尝试这样的事情怎么样:
var http = require('http');
var request = require('request');
var server = http.createServer(function(request, response){
response.writeHead(200, {"Content-Type": "text/plain"});
// response.write('on'); // why do you have this?
getSummonerId(function(data) { // notice that we pass in a function, but keep response handling up here
response.end(JSON.stringify(data)); // HTTP responses must be strings
});
});
server.listen(9000);
console.log("Server running!");
function getSummonerId(cb){
request('https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/' + 'someones-name' + '?api_key=mykeygoeshere', function(request, response, body){
cb(JSON.parse(body));
});
}