node.js fs.writeFileSync() 如何设置编码为big5?

node.js fs.writeFileSync() How to set encoding to big5?

fs.writeFileSync 编码默认为 UTF 8 我无法将编码设置为 big5。 该文档没有提到那些编码支持。 如果这个功能不支持BIG5,我该怎么办?

var fs = require('fs');
var FilePath='./text.txt';
var Str='this is a test!';
var encode='utf8';
fs.writeFileSync(FilePath, Str, encode);

当我设置编码(var encode='big5';) BIG5时,服务器产生错误。

使用不是 standard with Node Core. You can use iconv-lite 的编码。

它增加了对其他编码的支持,包括 big5here is the full list of encodings

const iconv = require('iconv-lite');
const fs = require('fs');
const stream = require('stream');

var Str = iconv.encode('This is a test', 'big5');

var readStream = new stream.PassThrough();
var writeStream = fs.createWriteStream('./text.txt');

readStream.once('error', (err) => { console.log(err); });    
readStream.once('end', () => { console.log('File Written'); });

readStream.end(Str); // write data to stream    
readStream.pipe(writeStream); // pipe data to file