Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fs.writeFile with 'wx' flag does not fail if file exists

Tags:

node.js

fs

I'm using FileCookieStore which requires a path to a cookiefile and that this file is already created. I want to use fs.writeFile to create this file if it does not already exist, but not to overwrite it if it does exist. However, in the implementation below, it overwrites the content of the cookiefile even if it exists, even though the wx -flag should throw an error and not overwrite the file if it already exists.

var cookiePath = "cookie.json"

fs.writeFile(cookiePath, null, { flags: 'wx' }, function (err) {
    if (err) throw err;
});

var j = request.jar(new FileCookieStore(cookiePath));
request = request.defaults({ jar : j });

function login(callback) {
    // puts data into j which automatically writes the content to cookiePath
}

Have I misunderstood the proper use of writeFile with the wx-flag? If I run login() the content of the cookie is automatically saved to cookie.json, but if I run the file again without calling login, fs.writeFile empties the cookiefile.

like image 238
tsorn Avatar asked Oct 19 '22 22:10

tsorn


1 Answers

your code should be:

fs.writeFile(cookiePath, null, { flag: 'wx' }, function (err)

:)

like image 194
I did just the same thing. Avatar answered Oct 21 '22 17:10

I did just the same thing.