2016-09-10 52 views
0

我是nodejs中的新成员。我试图通过显示HTML的NodeJS形式,这里是我的代码:javascript - 在node.js上显示html

function start(response){ 
 
\t console.log("Request handler 'start' was called."); 
 
\t var body = '<html>\n'+ 
 
\t '<head>\n'+ 
 
\t '<meta http-equiv="Content-Type" content = "text/html:'+ 
 
\t 'charset = UTF-8" />'+ 
 
\t '</head>'+ 
 
\t '<body>'+ 
 
\t '<form action="/upload" method="post">'+ 
 
\t '<textarea name="text" rows="20" cols="60"></textarea>'+ 
 
\t '<input type="submit" value="submit text" />'+ 
 
\t '</form>'+ 
 
\t '</body>'+ 
 
\t '</html>'; 
 
\t response.writeHead(200,{"Content-Type":"text/plain"}); 
 
\t response.write(body); 
 
\t response.end(); 
 
}

的问题是,当我运行的代码服务器只是给我的HTML源支票影像:

enter image description here

我的问题在哪里?

回答

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

你已经告诉你它发送纯文本浏览器,所以它呈现文档为纯文本。

如果您要发送HTML,那么请说它是HTML。

Content-Type: text/html 
0

这是因为你给的Content-Type

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

这是text/plain这意味着浏览器将呈现响应为PLAIN TEXT,而不是HTML。

您需要做的是设置有效的内容类型以将响应呈现为HTML

对于HTML正确的内容类型是text/html。所以,你的代码会是这个样子,

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

List of content types