I want to encode the data to windows1251.
My output json data is not good like that:
This is my node.js API code:
// Create express app
var express = require("express")
var app = express()
var mysql = require('mysql')
var express = require("express")
var cors = require('cors')
app.use(cors())
const utf8 = require('utf8');
// Server port
var HTTP_PORT = 3002
var pool = mysql.createPool({
connectionLimit: 10,
host: '',
user: '',
port: '',
password: '',
database: '',
charset: 'cp1251_bulgarian_ci'
});
var HQdataAHS = '';
var HQstationsAHS = '';
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
return [year, month, day].join('-');
}
var dateNow = formatDate(Date());
app.route('/HQstationsAHS')
.get(function (req, res) {
// omitted
res.setHeader('Access-Control-Allow-Origin', '*', 'Cache-Control', 'private, no-cache, no-store, must-revalidate');
pool.query(`SELECT Station, Ime FROM auto_q_stations;`, function (error, result2, fields) {
if (error)
return res.status(500).json({ error: "Грешна заявка. Опитай отново !" })
HQstationsAHS = result2.map((item) => {
item.Ime = utf8.encode(item.Ime);
return item;
});
res.json({ HQstationsAHS })
});
});
// Start server
app.listen(HTTP_PORT, () => {
console.log("Server running on port %PORT%".replace("%PORT%", HTTP_PORT))
});
pool.on('error', function (err) {
console.log(err.code); // 'ER_BAD_DB_ERROR'
});
app.use(function (req, res) {
res.status(404);
});
I installed from npm windows1251 library from here:
https://www.npmjs.com/package/windows-1251
After installation I try to encode this line like that:
item.Ime = utf8.encode(item.Ime);
item.Ime = windows1251.encode(item.Ime);
But I receive error: windows1251 is not defined..
How to encode my output json data to windows1251 ?