why does it not return JSON to browser

Problem

I am trying to send JSON format to browser to show. But it seems like it is not going to broweser plus there is an error like req.write part. The access url is 127.0.0.1:3000/media=video i can access to server but it dose not return JSON to browser.

var http = require('http');
var recommandCat=['top5','newest','like','random','recommened'];
var url =require('url');


http.createServer(function (req, res) {
  var url_parts = url.parse(req.url, true);
  var query = url_parts.query;
  var returnJson="";
  console.log(query.media);  
   switch (query.media) 
   {
    case 'photo':
        returnJson=photoGenerator();
        break;
        case 'video':
        returnJson=videoGenerator();
        break;
        case 'audio':
        returnJson=audioGenerator();
   }


   console.log(returnJson);
   res.write(returnJson);   


       }).listen(3000, '127.0.0.1');


    function videoGenerator()
   {
    var jsonArray = new Array();
    for(i=0;i<10;i++)
   {
    var data = new Object();         
     var recoCat = recommandCat[Math.floor(Math.random()*4)];        
     data = {
                    tomEngine:{ 
                    mediaType:"video",
                        recommendset:[

                                                    {recommendCat:"+recoCat+",

                                                    recommendResult:[

                                                   {

                                                    mediaId:"",

                                                    mediaEntry:[{
                                                        user1:{
                                                                    name:"sooin",
                                                                    rating:"3",
                                                                    views:"2",
                                                                    like:"ture",
                                                                    comment:"good"                                  
                                                        },
                                                        user2:{
                                                                    name:"sara",
                                                                    rating:"1",
                                                                    views:"4",
                                                                    like:"ture",
                                                                    comment:"good!"                                 
                                                        }                                       
                                                        }], 
                                                    view:"4",
                                                    rating:"4",
                                                    like:"10",
                                                    attribute:{
                                                                    smallUrl:"www",
                                                                    largeUrl:"llll",
                                                                    title:"aaaa"
                                                    },
                                             }]                                                 
                                               }
                        ]   
                    }
        };  
        jsonArray.push(data);
}

return jsonArray;
}

Maybe my json format is wrong. Thank you.

Problem courtesy of: draford

Solution

You probably have to send correct Content-Type header to client. Also response.end() should be called in the end of each response.

var body = JSON.stringify(returnJson);

res.writeHead(200, {
  'Content-Length': body.length,
  'Content-Type': 'application/json'
});

res.write(body);
res.end();

Also if the body of your response is not chunked, you can use only method response.end() like

res.end(body);
Solution courtesy of: Eugene Naydenov

Discussion

There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.