'How to get data from custom created function in JS using https node module
How to get data from my function getDashboardData() instead of JSON PLACE HOLDER mock API using HTTPS/HTTP node module and how to make an endpoint of this get data HTTP/HTTPS module to utilize response in front end just like Angular?
My mock backen.js file:
const https = require('https');
https.get(getDashboardData.data, res => {
let data = [];
const headerDate = res.headers && res.headers.date ? res.headers.date : 'no response date';
console.log('Status Code:', res.statusCode);
console.log('Date in Response header:', headerDate);
res.on('data', chunk => {
data.push(chunk);
});
res.on('end', () => {
console.log('Response ended: ');
const users = JSON.parse(Buffer.concat(data).toString());
for(user of users) {
console.log(`Got user with id: ${user.id}, name: ${user.name}`);
}
});
}).on('error', err => {
console.log('Error: ', err.message);
});
function getDashboardData() {
var data = {};
var dashboard1 = {};
dashboard1.orders = 10;
dashboard1.lastVisit = 70;
dashboard1.revenue = 70;
dashboard1.lastWeek = 70;
dashboard1.customers = 70;
dashboard1.newlyRegistered = 70;
dashboard1.comments = 70;
dashboard1.responded = 70;
dashboard1.storedAt = "2022/15/5 5:01:30";
data.dashboardData = [];
data.dashboardData.push(dashboard1);
return data;
}
Your time and help will be really appreciated. Thanks :)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
