How to set file encoding in Node.js

前端 未结 1 568
独厮守ぢ
独厮守ぢ 2021-01-29 08:30

I have some issues in set encoding in node.js. I tried to set encoding to utf8 using writeFile function. But no results. current file I want to set encoding using node.js wa

相关标签:
1条回答
  • 2021-01-29 08:50

    By default, the fs module will write files with an encoding of 'utf8'. UTF-8 is an encoding commonly used in web pages and other documents. File encoding refers to the character set that is used for the contents of the file. Commonly used encodings are 'utf8', 'ascii', 'binary', 'hex', 'base64' and 'utf16le'.

    Source from stackabuse.

    To write file with encoding

    If options is a string, then it specifies the encoding. Example:

    fs.writeFile('message.txt', 'Hello Node.js', 'utf8', callback);
    

    or is options is an object then,

    fs.writeFile('message.txt', 'Hello Node.js', {encoding: 'utf8'}, callback);
    

    Check here for more information

    0 讨论(0)
提交回复
热议问题