Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

173
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda