You can use 
fs.existsSync<http://nodejs.org/api/fs.html#fs_fs_existssync_path>but
I have read in this list several times it is discouraged, what happen
if the file is deleted from disk *after* you ask if it exists and
*before*you require it.

You will be better with a try/catch.


2013/1/29 Norman Khine <[email protected]>

> i have this code:
>
> var blade = require('blade')
>     ,express = require('express')
>     ,http = require('http')
>     ,https = require('https')
>     ,fs = require('fs'),json;
>
> 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'
> };
>
> function TZMNetwork(fileId) {
>     if (fs.existsSync("data/chapters.json")) {
>         // ... put code if "data/chapters.json" has changed!
>         console.log('OK');
>     } else {
>         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);
>         });
>     }
> }
>
> 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.set('views', __dirname + '/views'); //tells Express where our views
> are stored
> app.set('translation', require(__dirname +
> '/public/locales/dev/translation.json'));
> app.set('chapters', require(__dirname + '/data/chapters.json'));
> app.set('view engine', 'blade'); //Yes! Blade works with Express out of
> the box!
> app.get('/', function(req, res, next) {
>     TZMNetwork(TABLE_ID);
>     res.render('index');
> });
> app.listen(29080);
> console.log('Server running at http://127.0.0.1:29080/');
>
>
> How would you ensure that if /data/chapters.json does not exists, for
> example when the app is run for the first time, the app.set('chapters',
> require(__dirname + '/data/chapters.json')); is by-passed?
>
> 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 [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> 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 [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

-- 
-- 
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 [email protected]
To unsubscribe from this group, send email to
[email protected]
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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to