Possible Duplicate:
JSON array in Node.js
Im kinda new to node.js and Im working on only the server side to answer POST from the client side. What i need to do is when a POST request is made at location '/sort' with parameter 'theArray', sort the array removing all non-string values and return the resulting value as JSON. theArray parameter will be a stringified JSON Array. I have tried this code here:
case '/sort':
if (req.method == 'POST') {
res.writeHead(200,{
'Content-Type': 'application/json'
});
var fullArr = "";
req.on('data', function(chunk) {
fullArr += chunk;
});
req.on('end', function() {
var query = JSON.parse(fullArr);
var arr = "";
var par = query.theArray;
arr += par;
console.log(arr);
function censor(key, value) {
if (typeof value == "string") {
return value;
}
return undefined;
}
var jsonString = JSON.stringify(arr, censor);
console.log(jsonString);
});
res.end();
};
break;
But it just returns the same thing? I have also tried replacing the qs.parse with the JSON.parse and it just returned undefined? Can someone please help! Thanks
Check this:
wait till the request has ended.. then parse your fullArray
var data = ""
req.on('data',function(chunk){data+=chunk})
req.on('end',function(){...parse here....})