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

179
Views
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) {
        [...]
    });
[...]
over 4 years ago · Santiago Trujillo
1 answers
Answer question

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 = {}
over 4 years ago · Santiago Trujillo 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!