How to properly encode content in node.js?

Given the following code

var http = require('http'); http.createServer(function (request, response) { response.writeHead(200, {'Content-Type': 'text'}); response.write("Okay โ€“ so recently I've started presenting a session to various groups involving the well known IOC container "StructureMap"", 'utf8'); response.end(); }).listen(8080); 

I get a conclusion

Well, so recently I started presenting the session to various groups with the participation of the well-known IOC container "Structure Structure" and, despite quite clear about the content from what was said, I will get very little backlash to demonstrate anything that even remotely resembles a service location, etc.

This is clearly wrong - but it is wrong because I have the wrong encoding (should UTF8 do this ... right? ... right?) Or is it wrong because node is doing something weird?

I am using the latest version of node cloned from github yesterday.

+8
javascript encoding
source share
1 answer

If you want it to be loaded as a text file with proper encoding, you should use the content type text/plain; charset=utf-8 text/plain; charset=utf-8 . Just using text not enough. I just tested it and it works as expected. Change plain to html so that the browser uses the default styles in the text.

+10
source share

All Articles