i have this code from an node.js/express application i am trying to develop:
var blade = require('blade') ,express = require('express') ,http = require('http') ,https = require('https') ,fs = require('fs') ,nowjs = require('now') ,City = require('geoip').City,json; var city = new City('data/GeoLiteCity.dat' ); var GOOGLE_API_KEY = process.env.GOOGLE_API_KEY; var TABLE_ID = "1epTUiUlv5NQK5x4sgdy1K47ACDTpHH60hbng1qw"; //var TABLE_ID ="1obpi0bmSDILX1cIQcVRNi1lUkm2K5xBFztmRFiM" var GOOGLE_PATH = "/fusiontables/v1/query?sql=SELECT%20*%20FROM%20"+TABLE_ID+"&key="+GOOGLE_API_KEY; var GOOGLE_DRIVE_PATH = "/drive/v2/files/"+TABLE_ID+"?key="+GOOGLE_API_KEY; var options = { hostname: 'www.googleapis.com' ,port: 443 ,method: 'GET' }; var lastModifiedDate = ''; TZMNetwork(TABLE_ID); function TZMNetwork(fileId) { if (fs.existsSync("data/chapters.json")) { var x = ''; options["path"] = GOOGLE_DRIVE_PATH; var req = https.request(options, function(res) { res.on('data', function(d) { x += d.toString(); //console.log(d.toString()); }).on('end', next); }); req.end(); req.on('error', function(e) { console.error(e); }); function next() { var l = JSON.parse(x); var modifiedDate = l.modifiedDate.toString(); if (!lastModifiedDate === modifiedDate) { console.log('chapters.json is out of date'); getChapters(); } //console.log(l.modifiedDate); //options['modifiedDate'] = l.modifiedDate.toString(); lastModifiedDate += l.modifiedDate.toString(); } console.log('OK'); } else { getChapters(); } } function getChapters() { options["path"] = GOOGLE_PATH; var file = fs.createWriteStream("data/chapters.json"); var req = https.request(options, function(res) { res.on('data', function(data) { file.write(data); }).on('end', function() { file.end(); console.log("chapters.json created"); }); }); req.end(); req.on('error', function(e) { console.error(e); }); } function dumpError(err) { if (typeof err === 'object') { if (err.message) { console.log('\nMessage: ' + err.message) } if (err.stack) { console.log('\nStacktrace:') console.log('====================') console.log(err.stack); } } else { console.log('dumpError :: argument is not an object'); } } var app = express(); app.use(blade.middleware(__dirname + '/views') ); //for client-side templates app.use(express.favicon(__dirname + '/public/images/favicon.ico')); app.use(express.static(__dirname + '/public') ); //maybe we have some static files //app.use(blade.middleware(__dirname + '/views') ); //for client-side templates app.use(express.static(__dirname + "/public") ); //maybe we have some static files app.set('views', __dirname + '/views'); //tells Express where our views are stored try { app.set('translation', require(__dirname + '/public/locales/dev/translation.json')); app.set('chapters', require(__dirname + '/data/chapters.json')); } catch(err) { dumpError(err); console.log('there is no /data/chapters.json'); app.set('chapters', []); } app.set('view engine', 'blade'); //Yes! Blade works with Express out of the box! app.get('/', function(req, res, next) { TZMNetwork(TABLE_ID); console.log(lastModifiedDate); res.render('index'); }); app.get( '/map', function( req, res, next ) { var ip = ( req.connection.remoteAddress !== "127.0.0.1" )? req.connection.remoteAddress: "72.196.192.58"; city.lookup( ip, function( err, loc ) { if ( err ) { loc = {}; } res.render( 'map', { loc: loc } ); }); }); app.locals.pretty=true; app.listen(29080); console.log('Server running at http://127.0.0.1:29080/'); basically when the applications starts, i have this try/catch: try { app.set('translation', require(__dirname + '/public/locales/dev/translation.json')); app.set('chapters', require(__dirname + '/data/chapters.json')); } catch(err) { dumpError(err); console.log('there is no /data/chapters.json nor translation.json'); app.set('translation', []); app.set('chapters', []); } which checks if these files exist, if not it sets and empty array! what will be the correct way to call the TZMNetwork()function and then reset the app.set('chapters', require(__dirname + '/data/chapters.json')); or do the 'try'/catch again? also, the TZMNetwork()function checks to see if the lastModifiedDate has changed and if it has then it creates a new chapters.json file; this check is also done when the user accesses the root of the application. is there a better way to do this? -- -- Job Board: http://jobs.nodejs.org/ Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines You received this message because you are subscribed to the Google Groups "nodejs" group. To post to this group, send email to nodejs@googlegroups.com To unsubscribe from this group, send email to nodejs+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en --- You received this message because you are subscribed to the Google Groups "nodejs" group. To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.