Milad
Milad

Reputation: 25

javascript - display html on node.js

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 ?

Upvotes: 0

Views: 230

Answers (2)

Alok Patel
Alok Patel

Reputation: 8022

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

Upvotes: 0

Quentin
Quentin

Reputation: 943556

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

Upvotes: 1

Related Questions