How to put express get json into html and make it look good?

Viewed 64
  app.get("/view", (req, res) =>{
request('https://corona.lmao.ninja/countries', (error, response, body) => {
console.error('error:', error); 
console.log('statusCode:', response && response.statusCode); 
console.log('body:', body); 
return res.send(body);
});
});

I have this code working and it gives me an json in my browser. I am new to programing and have no idea how i put this in html or javascript to make a nice looking website.

3 Answers

Start simple. First determine if the body coming back from request() is an object or a string. If it's a string, then you need to parse is using obj=JSON.parse(body) to get an object.

From there you can try the quick and dirty:

res.send(`<html><body>Hi! Here is my json: <span color='red'> ${JSON.toString(obj)}</span></body></html>`)

That should get you started. The next step would be to output various obj fields to separate HTML elements (spans, divs, tables, whatever).

obj=JSON.parse(body)
var jsonstr=JSON.stringify(obj)
document.body.innerHTML=jsonstr
Related