Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

175
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda