Using Node.js and am fairly new to the software. However this is something that bothered me from my vanilla front-end days. I have a simple html.get stream
https.get(url, (res)=>{
res.setEncoding('utf8');
let body = '';
res.on('data', (data)=>{
body += data;
});
res.on('end', ()=>{
body = JSON.parse(body);
//console.log(typeof body);
callback(body);
})
})
with the callback function just returning the file to my webpage
function callback(body){
res.send(body);
}
The problem that I have with this is that the file that I send to my server, i.e. var = body , is that the file is... not easily readable. I'm not sure if that makes complete sense so this might be a better explanation:
This is what I want:
{
"title": "Google Custom Search - lectures",
"totalResults": "970000000",
"searchTerms": "lectures",
"count": 10,
"startIndex": 11,
"inputEncoding": "utf8",
"outputEncoding": "utf8",
"safe": "off",
"cx": "[REDACTED]"
}
with all the whitespaces intact. This is the what the URL for the get gives. However after receiving it and concatenating/parsing it with my algorithm I get:
{"title":"Google Custom Search - lectures","totalResults":"970`0000","searchTerms":"lectures","count":10,"startIndex":11,"inputEncoding":"utf8","outputEncoding":"utf8","safe":"off","cx":"[REDACTED]"}]}
without any white-spaces or anything. It just comes out as 'blocks of texts'. I (HOPE) I did something wrong and there is a way to preserve the original format when I use the html.get call.
My original plan was to use some 'regex magic' to format it to become more readable, but there should be another way I hope considering the original URL has it formatted it properly. I'm dealing with a big JSON file, so a more readable format would be nice. I'm not sure if I explained my scenario perfectly, this is my first Stack question, but if its not please let me know. Thanks!