Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7081249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:53:47+00:00 2026-05-28T06:53:47+00:00

The http.get() function inside http.createServer is not responding. I wrote a small snippet to

  • 0

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.com',
        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.org%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.onIncoming (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 come 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.org%2Falltableswithkeys&callback=cbfunc';

http.get({
    host: 'query.yahooapis.com', 
    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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:53:48+00:00Added an answer on May 28, 2026 at 6:53 am

    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.com',
            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.org%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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

$(function(){ $.getJSON('http://ws.audioscrobbler.com/2.0/?method=user.getweeklyartistchart&user=ElicitBelief&api_key=25135a535781328243f9e31968abc14&format=json', function(data) { alert(data) }); }); Firebug says: GET http://ws.audioscrobbler.com/2.0/?method=user.getweeklyartistchart&user=ElicitBelief&api_key=25135a535781328243f9e31968abc14&format=json 200 OK 144ms
Is it better to use $.get(http://www.example.com/mydirectory, function(data) { $(.someclass).html(data); }); or $('.tripPlannerBottom').load(http://www.example.com/mydirectory); any speed
I try to get this following url using the downloadURL function as follows: http://www.ncbi.nlm.nih.gov/nuccore/27884304
I want to use HTTP GET and POST commands to retrieve URLs from a
I need to get current perl thread id in a C function inside *.XS
I get one tutorial over here http://codeigniter.com/forums/viewthread/122597/P0/ everything is find but not the pagination
func.js : $.get( test.php, {id: theid}, function(data){...} ) My test.php and func.js files are
I am trying to use the http_get function. But I get a undefined reference
As in http://nant.sourceforge.net/release/latest/help/functions/nant.get-base-directory.html , they explaint the meaning of this function is: The base
An HTTP GET query string is a ordered sequence of key/value pairs: ?spam=eggs&spam=ham&foo=bar Is,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.