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

169
Visualizações
Pass json file to client using Express routes

I'm trying to send a json file to the client, but I fail to do so. I get internal errors (500). Can someone help me out? Also, I use Handlebars as view engine.

app.js:

[...]
var APIconditions = {};
var APIhourly = {};

function getWeatherData() {
    var data = ['/conditionsWU.json', '/hourlyWU.json'];
    request(data[0], function(err, res, body) {
        if (!err && res.statusCode == 200) {
            APIconditions = JSON.parse(body);
        }
    });
    request(data[1], function(err, res, body) {
        if (!err && res.statusCode == 200) {
            APIhourly = JSON.parse(body);
        }
    });
}
[...]

index.js:

[...]
router.get('/conditions' , function(req, res, next) {
    res.json(APIconditions);
});

router.get('/hourly' , function(req, res, next) {
    res.json(APIhourly);
});
[...]

client.js:

[...]
function getWeather() {
    var APIconditions = 'conditions';
    var APIhourly = 'hourly';

$.getJSON(APIconditions, function (data) {
    [...]
});
$.getJSON(APIhourly, function (data) {
        [...]
    });
[...]
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can't access APIconditions or APIhourly from another file without specifying it.

The simplest solution would be to attach them to the global scope, so you have access to them from everywhere.

global.APIconditions = {}
global.APIhourly = {}
about 4 years ago · Santiago Trujillo 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