I have this code:
var qs = require('querystring');
var http = require('http');
http.createServer(function (req, res) {
if ('/' == req.url) {
res.writeHead(200, { 'Content-Type': 'text/html'});
res.end([
'<form method="post" action="/url">',
'<h1>My form</h1>',
'<fieldset>',
'<label>Personal information</label>',
'<p>What is your name?</p>',
'<input type="text" name="name">',
'<p><button>Submit</button></p>',
'</form>'
].join(''));
} else if ('/url' == req.url && 'POST' == req.method) {
var body = '';
req.on('data', function (chunk) {
body += chunk;
});
req.on('end', function () {
res.writeHead(200, { 'Content-Type': 'text/html'});
res.end('<p>Your name is: <strong>' + qs.parse(body).name + '</strong></p>');
});
}
}).listen(3000);
Lets say i write my swedish name "Anders Östman" name in the input field and POST the form. Everything seems to work fine, except that my name is output as Anders �stman. The charatcher "Ö" get trashed... I guess this have something to do with character encoding and I need to set/convert the parsed object to UTF-8.
Q: Is there a way to qs.parse() directly into a UTF-8 object? Or does object does'nt have encoding? Do i need to encode the object value when i output it instead?