1

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?

2
  • 1
    Is your second res a typo? It seems you are using response instead ... Commented Oct 11, 2014 at 21:01
  • Nope, see comment below. Commented Oct 11, 2014 at 21:07

1 Answer 1

1

Try:

res.writeHead(200, { 'Content-Type': 'text/html' });

And then:

response.setEncoding('utf8');
response.on('data', function(chunk) {
    body += chunk;
});
Sign up to request clarification or add additional context in comments.

5 Comments

thanks. Please see the update above, I'm getting an error related to the character encoding on the page.
Error: res has no method setEncoding
Look at @mscdex's comment. You have a nested res that probably needs to be renamed to response.
No, the response object passed into http.request's callback does not have a write method. Only the http.createServer's callback does. It seems strange but apparently its how node works.
Great, that worked. I'm still having issues, but they are not related to this thread : stackoverflow.com/questions/26310708/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.