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
Add a ment  | 

1 Answer 1

Reset to default 3

node.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