Why does Node.js' fs.readFile() return a buffer instead of string?

后端 未结 6 981
梦毁少年i
梦毁少年i 2020-11-30 17:33

I\'m trying to read the content of test.txt(which is on the same folder of the Javascript source) and display it using this code:

var fs = requi         


        
6条回答
  •  情书的邮戳
    2020-11-30 18:00

    From the docs:

    If no encoding is specified, then the raw buffer is returned.

    Which might explain the . Specify a valid encoding, for example utf-8, as your second parameter after the filename. Such as,

    fs.readFile("test.txt", "utf8", function(err, data) {...});
    

提交回复
热议问题