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) {
[...]
});
[...]
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 = {}