3

I'm working through the tutorial in the beginner node book and I'm putting in this code.

    var body = '
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" charset="UTF-8"/>
</head>

<body>
    <form action="/upload" method="post">
        <textarea name="text" rows="20" cols="60"></textarea>
        <input type="submit" value="Submit text"/>
    </form>
</body>
</html>';
response.writeHead(200,{"Content-Type" : "text/plain"});
response.write(body);
response.end();

I've spaced it out, but I know I need to put it all on one line, when I do, the textarea does not appear, the html as written appears on screen.

I've spaced it out because I'm not sure if the html is correct.

What's going wrong?

2
  • "I think I need to put it all on one line" - Yes, or concatenate several strings together (one per line). Have you tried "text/html" instead of "text/plain" (on the third-last line)? Commented Jan 19, 2013 at 10:53
  • right, I know, but I spaced it out just in case there was a problem with the html I had typed... Commented Jan 19, 2013 at 10:54

1 Answer 1

18

Try changing this line:

response.writeHead(200,{"Content-Type" : "text/plain"});

To:

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

(You want the browser to interpret it as html, not plain text.)

Sign up to request clarification or add additional context in comments.

4 Comments

nice, this is exactly what I wanted
But there is a problem, if you do following: res.writeHead(200,{"Content-Type" : "text/html"}); res.write('<b>How to render/load pages (views)? </b> <br/><br/>'); res.write('<span style="color:#259dff">localhost:'+port+'/products </span>'); res.write('will load (render)'); res.write('<span style="color:#777">localhost:'+port+'/products.html</span> page/view.'); res.end(); It does not display http: written in the text.
@MuhammadHannan - Well, why should it? You don't include 'http:' in your string. In any case, that appears to be unrelated to this question, so perhaps you should post a new question of your own.
Wow thanks for that @nnnnnn. I had the same problem!

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.