Last active
May 25, 2016 02:45
-
-
Save huguangju/d365bd1c459b50cec74630a1919781d0 to your computer and use it in GitHub Desktop.
Check if a file no exist in NodeJS, create it
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
module.exports = function checkFileAndCreate(cb) { | |
try { | |
fs.accessSync(file, fs.F_OK); | |
cb(null); | |
} catch (err) { | |
if(err.code === 'ENOENT') { | |
fs.writeFileSync(file, ''); | |
cb(null); | |
}else { | |
cb(err); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment