3
我使用FileCookieStore
,它需要一個cookie文件的路徑並且該文件已經創建。我想使用fs.writeFile
創建該文件,如果它不存在,但不覆蓋它,如果它存在確實存在。但是,在下面的實現中,即使存在cookiefile,它也會覆蓋cookie文件的內容,即使wx
-flag應該拋出一個錯誤,並且如果文件已經存在則不會覆蓋該文件。如果文件存在,使用'wx'標誌的fs.writeFile不會失敗
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
}
有我誤解的writeFile
與wx
-flag正確使用?如果我運行login()
,cookie的內容會自動保存到cookie.json中,但是如果我再次運行文件時未調用login
,則fs.writeFile
會清空cookie文件。