Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

174
Views
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 :)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Hurray! I got it using the following code and Express in node js. I simply call my custom method that creates data into an express "get" endpoint. When I hit the end point the response will be my custom method result.

const express = require('express');
const cors = require('cors');

const app = express();
app.use(cors());

app.get('/data', (req, res) => {
res.send(
  getDashboardData());
});

app.listen(3000, () => {
    console.log('server is listening on port 3000');
});



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);
   //console.log(JSON.stringify(data));
   return JSON.stringify(data);
 }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!