2015-11-04 53 views
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 
} 

有我誤解的writeFilewx -flag正確使用?如果我運行login(),cookie的內容會自動保存到cookie.json中,但是如果我再次運行文件時未調用login,則fs.writeFile會清空cookie文件。

回答

0

WriteFile的方法的語法按文檔fs.writeFile(file, data[, options], callback)

我可以看到你發送null來代替要寫入的數據。這可能是您的空cookie文件背後的原因。

相關問題