If I run the code below with the "setEncoding" line uncommented, I receive the following error:
/usr/local/test-server/test.js:78
res.setEncoding('utf8');
^
TypeError: Object # has no method 'setEncoding'
Without that line, everything works as expected - except for browsers complaining about undeclared character encoding.
Nothing in the docs, SO, the GitHub issues list, or extensive Googling have turned up anything useful. node.js version is the latest: 0.8.6
var https = require('https');
var sslPrivateKey = fs.readFileSync('./pk.pem');
var sslCert = fs.readFileSync('./cert.pem');
var sslOpts = { key: sslPrivateKey, cert: sslCert };
var server = https.createServer(sslOpts, function(req, res) {
if ('GET' === req.method) {
res.writeHead(200, {'Content-Type': 'text/plain','charset': 'utf8'});
//res.setEncoding('utf8');
res.write('You are here' + "\n");
res.end();
}
}
server.listen(8080);
Class http.ServerResponse has no setEncoding method. You trying to set header 'charset' but 'charset' is part of 'Content-Type' header. Try this:
res.writeHead(200, {'Content-Type': 'text/plain; charset=utf8'});
Remember that this is just information for client about how to interpret the contents, not rule.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With