When I'm reading lines from file I'm trying to print some strings which contain english and russian words. I'm getting replacement characters (�����) instead cyrillic symbols in my console. If I'm trying to return an array of lines I'm getting same trouble.
A plain line looks like:
URL: GET /products/1234
Message: [transaction ID: 324] Отправка Get запроса: http://...//URL: POST /products/1234
Message: [transaction ID: 324] Отправка Post запроса: http://...//
Console prints:
URL: GET /products/1234
Message: [transaction ID: 324] ���������������: http://...//URL: POST /products/1234
Message: [transaction ID: 324] ���������������: http://...//
The same is written to the array(� instead lines with cyrillic chars):
["URL: GET /products/1234 Message: [transaction ID: 324 ���������������: http://...//","URL: POST /products/1234 Message: [transaction ID: 324 ���������������: http://...//", ]
How can I fix it?
//function to watch for file's changes:function readFileChanges(file) {const Tail = require('tail').Tail;const tail = new Tail(file);let dataLines = [];tail.watch();tail.on("line", data => {console.log(data);dataLines.push(data);});return dataLines;};
I expect to get array with lines containing cyrillic chars, not �-s:
["> URL: GET /products/1234 Message: [transaction ID: 324] Отправка Get запроса: http://...//","> URL: POST /products/1234 Message: [transaction ID: 324] Отправка Post запроса: http://...//"]
and appropriate console-output.
Platform is win7 and win10, node version is 10.x
Thank you so much in advance!
Best Answer
Try this:
const iconv = require('iconv-lite');const Tail = require('tail').Tail;function readFileChanges(file) {const tail = new Tail(file, {encoding: "binary"});let dataLines = [];tail.watch();tail.on("line", data => {data = iconv.decode(data, "cp1251").toString();console.log(data );dataLines.push(data);});return dataLines;};