使用uft8模块
安装:npm install utf8
Browser

  1. <script src="utf8.js"></script>

Node.js

const utf8 = require('utf8');

Encode:

utf8.encode(string)

// U+00A9 COPYRIGHT SIGN; see http://codepoints.net/U+00A9
utf8.encode('\xA9');
// → '\xC2\xA9'
// U+10001 LINEAR B SYLLABLE B038 E; see http://codepoints.net/U+10001
utf8.encode('\uD800\uDC01');
// → '\xF0\x90\x80\x81'

Decode

utf8.decode(byteString)

utf8.decode('\xC2\xA9');
// → '\xA9'

utf8.decode('\xF0\x90\x80\x81');
// → '\uD800\uDC01'
// → U+10001 LINEAR B SYLLABLE B038 E

参考资料:

https://stackoverflow.com/questions/20174280/nodejs-convert-string-into-utf-8