I am forwarding a request made from a proxy server and rendering the response in the browser. However, when i render the response with res.write(data), it comes up as plaintext instead of rendered html:
http.createServer(function targetServer(req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
var request = http.request('http://www.google.com', function(res) {
var body = '';
response.on('data', function(chunk) {
body += chunk;
});
response.on('end', function() {
fs.writeFile('res.txt', body, {encoding:'utf8'}, function() {
util.puts('Success!');
});
res.write(body);
});
});
request.end();
});
the contents of res.txt are:
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en"><head><meta content="Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking for." name="description"><meta content="noodp" name="robots"><meta content="/images/google_favicon_128.png" itemprop="image"><title>Google</title>
<!-- other html -->
</html>
but instead of rendering like google.com, it looks like the res.txt file:
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en"><head><meta content="Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking for." name="description"><meta content="noodp" name="robots"><meta content="/images/google_favicon_128.png" itemprop="image"><title>Google</title>
<!-- other html -->
</html>
Can someone explain to me what I'm doing wrong?
UPDATE
After changing the content type to text/html, i now get an error in the browser:
The character encoding of the plain text document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the file needs to be declared in the transfer protocol or file needs to use a byte order mark as an encoding signature.
Whats this about?
resa typo? It seems you are usingresponseinstead ...