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

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

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 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