admin管理员组文章数量:1421196
The http.get()
function inside http.createServer is not responding.
I wrote a small snippet to retrieve JSON data when a user send a request to the server. Here is my code.
var http = require('http');
var x = '';
http.createServer(function (request,response) {
http.get({
host:'query.yahooapis',
path:'/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22YHOO%22%2C%22AAPL%22%2C%22GOOG%22%2C%22MSFT%22)&format=json&env=store%3A%2F%2Fdata tables%2Falltableswithkeys&callback=cbfunc'
}, function(res){
res.on('data',function(d){
x+=d.toString();
console.log(d.toString());
})
});
response.writeHead(200, {'Content-Type':'text/plain'})
var l = JSON.parse(x.substring(7,x.length-2));
response.end(l.query.results.quote[0].symbol + '');
}).listen(8080);
I am getting the error:
undefined:0
SyntaxError: Unexpected end of input
at Object.parse (native)
at Server.<anonymous> (C:\Users\Lenovo\Documents\fetch.js:18:12)
at Server.emit (events.js:70:17)
at HTTPParser.onIning (http.js:1491:12)
at HTTPParser.onHeadersComplete (http.js:102:31)
at Socket.ondata (http.js:1387:22)
at TCP.onread (net.js:354:27)
As far as I think. Error is due to x='' is not a json so its throwing an error. But when sending a request by calling localhost:8080 it should show some json text on console instead of error. My aim is to parse the stock quotes in real time so i made a get request when request e and set its result to response.
i tried one more snippet for getting the data
var http = require('http');
var x = '/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22YHOO%22%2C%22AAPL%22%2C%22GOOG%22%2C%22MSFT%22)&format=json&env=store%3A%2F%2Fdata tables%2Falltableswithkeys&callback=cbfunc';
http.get({
host: 'query.yahooapis',
path: ''
},function(res){
res.on('data',function(d){
x+=d.toString();
console.log(d.toString());
})
});
http.createServer(function(request,response){
response.writeHead(200,{'Content-Type':'text/plain'})
var l=JSON.parse(x.substring(7,x.length-2));
response.end(l.query.results.quote[0].symbol+'');
}).listen(8080);
It's working fine showing json text on console but I think I will get the data for once when I deployed it on server not when user is requesting the data. How can I solve the error?
The http.get()
function inside http.createServer is not responding.
I wrote a small snippet to retrieve JSON data when a user send a request to the server. Here is my code.
var http = require('http');
var x = '';
http.createServer(function (request,response) {
http.get({
host:'query.yahooapis.',
path:'/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22YHOO%22%2C%22AAPL%22%2C%22GOOG%22%2C%22MSFT%22)&format=json&env=store%3A%2F%2Fdata tables%2Falltableswithkeys&callback=cbfunc'
}, function(res){
res.on('data',function(d){
x+=d.toString();
console.log(d.toString());
})
});
response.writeHead(200, {'Content-Type':'text/plain'})
var l = JSON.parse(x.substring(7,x.length-2));
response.end(l.query.results.quote[0].symbol + '');
}).listen(8080);
I am getting the error:
undefined:0
SyntaxError: Unexpected end of input
at Object.parse (native)
at Server.<anonymous> (C:\Users\Lenovo\Documents\fetch.js:18:12)
at Server.emit (events.js:70:17)
at HTTPParser.onIning (http.js:1491:12)
at HTTPParser.onHeadersComplete (http.js:102:31)
at Socket.ondata (http.js:1387:22)
at TCP.onread (net.js:354:27)
As far as I think. Error is due to x='' is not a json so its throwing an error. But when sending a request by calling localhost:8080 it should show some json text on console instead of error. My aim is to parse the stock quotes in real time so i made a get request when request e and set its result to response.
i tried one more snippet for getting the data
var http = require('http');
var x = '/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22YHOO%22%2C%22AAPL%22%2C%22GOOG%22%2C%22MSFT%22)&format=json&env=store%3A%2F%2Fdata tables%2Falltableswithkeys&callback=cbfunc';
http.get({
host: 'query.yahooapis.',
path: ''
},function(res){
res.on('data',function(d){
x+=d.toString();
console.log(d.toString());
})
});
http.createServer(function(request,response){
response.writeHead(200,{'Content-Type':'text/plain'})
var l=JSON.parse(x.substring(7,x.length-2));
response.end(l.query.results.quote[0].symbol+'');
}).listen(8080);
It's working fine showing json text on console but I think I will get the data for once when I deployed it on server not when user is requesting the data. How can I solve the error?
Share Improve this question edited Jan 25, 2017 at 11:07 Brainjump 867 bronze badges asked Jan 17, 2012 at 10:38 mathlearnermathlearner 7,66932 gold badges132 silver badges191 bronze badges 1- In your first example the last line shouldn’t be there? – Zoe Edwards Commented Jan 17, 2012 at 10:48
1 Answer
Reset to default 3node.js is asynchronous. This means that http.get
will return at some point. You send of a http.get
request and then immediatly try to manipulate the x
that your only writing to once the http.get
request finishes.
Basically x === ''
when your calling JSON.parse
because the http.get
callback doesn't fire until later.
var http = require('http');
var x = '';
http.createServer(function(request, response) {
http.get({
host: 'query.yahooapis.',
path: '/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22YHOO%22%2C%22AAPL%22%2C%22GOOG%22%2C%22MSFT%22)&format=json&env=store%3A%2F%2Fdatatables%2Falltableswithkeys&callback=cbfunc'
}, function(res) {
res.on('data', function(d) {
x += d.toString();
console.log(d.toString());
});
res.on('end', next);
});
function next() {
response.writeHead(200, {
'Content-Type': 'text/plain'
})
var l = JSON.parse(x.substring(7, x.length - 2));
response.end(l.query.results.quote[0].symbol + '');
}
}).listen(8080);
The important thing here is to wait until the http.get
call has finished until you send the json response.
本文标签: javascriptNodejs Httpget() function is not respondingStack Overflow
版权声明:本文标题:javascript - Node.js. Http.get() function is not responding. - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745333287a2653919.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论