javascript - Convert character set from none-standard one to UTF-8

There is a page with<meta charset="EUC-KR">
, say address-search.foo.com that searches an address and sends it to a specified url by submitting html form of method POST like below.
let form = <form element>;
form.zipCode.value = "63563";
form.address.value = "м њмЈјнЉ№лі„мћђм№?лЏ„ м„њк·ЂнЏ¬м‹њ мќґм–ґлЏ„лЎњ 579 (к°•м •лЏ™)";
form.action = "https://my-service.foo.com";
form.method = "post";
form.submit();
And there is a POST handler in my-service.foo.com run by ExpressJs to take the above request like below.
const next = require("next");
const app = next(nextConfig);
const server = express();
server.use(bodyParser.urlencoded({ extended: false }));
server.use(bodyParser.json());
server.post("/", (req, res) => {
console.log(req.body);
app.render(req, res, "/");
});
And the console.log(req.body); above prints below.
[Object: null prototype] {
zipCode: '63563'
address: 'пїЅпїЅпїЅпїЅЖЇпїЅпїЅпїЅпїЅДЎпїЅпїЅ пїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅ пїЅМѕоµµпїЅпїЅ 579 (пїЅпїЅпїЅпїЅпїЅпїЅ)'
}
I tried to convert the encoding ofreq.body.address
usingiconv-lite
module, but it doesn't work as it does on PHP like below.
iconv("CP949", "UTF-8", $_POST['address']); // working very happily
How to properly useiconv-lite
or is there anyway to get around this on ExpressJs?
Answer
Solution:
Use urlencode module.
I solved it by usingbodyParser.raw()
instead ofbodyParser.urlencoded()
.
const urlencode = require("urlencode");
const iconv = require("iconv-lite");
const qs = require("querystring");
server.use(
bodyParser.raw({ type: "application/x-www-form-urlencoded" }),
(req, res, next) => {
if (req.method === "POST") {
const decoded = iconv.decode(req.body, "utf8");
/**
* Define another conditional statement that filters an encoding specific case.
* Below if statement is just for my case.
*/
if (req.path === "/some/path/to/treat/differently") {
req.body = urlencode.parse(decoded, { charset: "euc-kr" })
}
else {
req.body = qs.parse(decoded);
}
}
next();
}
);
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: videoxxx
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.