2016-09-10 55 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