Last active
August 22, 2017 23:22
-
-
Save DylanCh/6ce329473f70324139c21c575a046266 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var axios = require('axios'); | |
var url = "<THE LINK TO THE ENDPOINT OF API THAT HOSTS THE ABOVE DATA>"; | |
// the HTML markup that begins with headers, | |
//references to CSS and Javascript libraries, etc | |
var html = '<html><head>' | |
/* Add references here (I am using Bootstrap and jQuery)*/ | |
+'</head><body class="container">' | |
+'<div class="row">' | |
+'<table class="table">' | |
+'<thead>' | |
+'<tr>'; | |
app.get(['/apple','/Apple'],(req,res)=>{ | |
axios.get(url). | |
then((result)=>{ | |
// first, use a data structure to hold the result data | |
var data = []; | |
data = result.data; | |
// second step, create a table header using object keys | |
Object.keys(data['Products'][0]).forEach(element=>{ | |
html+=`<th>${element}</th>` | |
}); | |
html+='</tr></thead><tbody>'; | |
// third step, create a table, and fill it with data | |
// using place holders | |
data.forEach(element=>{ | |
html+= `<tr> | |
<td>element['name']</td> | |
<td>element['category']</td> | |
</tr>` | |
}); | |
// next, close the open tags | |
html += '</tbody></table></body></html>'; | |
// Set response content type, very important | |
// otherwise it'll just send html as a string | |
res.setHeader('content-type', 'text/html'); | |
// Finally, send the response to the client | |
res.send(html); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment