最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - fs writefile new line not working - Stack Overflow

programmeradmin1浏览0评论

i want log my user command

function saveLog (nick, command) {
    var file = 'log/' + nick + '.log';
    var datetime = '[' + getDateTime() + '] ';
    var text = datetime + command + '\r\n';
    fs.writeFile(file, text, function (err) {
        if (err) return console.log(err);
        console.log(text);
    });
}

the function i made is fine, but it didnt save the log in new line, its just replace text / rewrite the file. whats im missing ?

thanks

i want log my user command

function saveLog (nick, command) {
    var file = 'log/' + nick + '.log';
    var datetime = '[' + getDateTime() + '] ';
    var text = datetime + command + '\r\n';
    fs.writeFile(file, text, function (err) {
        if (err) return console.log(err);
        console.log(text);
    });
}

the function i made is fine, but it didnt save the log in new line, its just replace text / rewrite the file. whats im missing ?

thanks

Share Improve this question asked May 17, 2015 at 4:54 anonprophetanonprophet 971 gold badge3 silver badges11 bronze badges 1
  • 1 You need fs.appendFile – laggingreflex Commented May 17, 2015 at 4:58
Add a comment  | 

1 Answer 1

Reset to default 19

fs.writeFile writes a WHOLE NEW file. What your are looking for is fs.appendFile which will make the file if it doesn't exist and append to it. Documentation here.

function saveLog (nick, command) {
    var file = 'log/' + nick + '.log';
    var datetime = '[' + getDateTime() + '] ';
    var text = datetime + command + '\r\n';
    fs.appendFile(file, text, function (err) {
        if (err) return console.log(err);
        console.log('successfully appended "' + text + '"');
    });
}
发布评论

评论列表(0)

  1. 暂无评论