0

i'm new in nodejs. i'm trying to show html form via nodejs,here is my code :

function start(response){
	console.log("Request handler 'start' was called.");
	var body = '<html>\n'+
	'<head>\n'+
	'<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();
}

the problem is when i run the code server just give me html source check image :

enter image description here

where is my problem ?

2 Answers 2

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

You've told the browser that you are sending it plain text, so it is rendering the document as plain text.

If you are sending HTML, then say it is HTML.

Content-Type: text/html
Sign up to request clarification or add additional context in comments.

Comments

0

That's because of the Content-Type you've given.

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

It's text/plain which means browser will render the response as PLAIN TEXT instead of HTML.

What you need to do is set Valid content type to render the response as HTML.

For HTML correct Content Type is text/html. So your code would look something like this,

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

List of content types

Comments

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.