我已经创建了我的第一个 Node js 应用程序:一个简单的网络服务器。 这是代码:
// Load the http module to create an http server.
var http = require('http');
// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("<a href=\"http://www.tagesschau.de/\">ARD Tagesschau</a>\n");
});
// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8000);
// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:8000/");
当我通过浏览器连接到服务器时,我得到了我的代码中指定的完整字符串作为网页。
浏览器不应该解释该 HTML 代码并显示链接吗?为什么我得到的完整 HTML 代码显示为纯文本?
最佳答案
您明确表示要返回纯文本,而不是 HTML。因此,浏览器将其视为纯文本。
如果您希望 HTML 被视为 HTML,则说它是 HTML:
{"Content-Type": "text/html"}
(尽管您应该发回 HTML 文档而不是 HTML 片段)。
关于javascript - 来自 node js 的 HTML 代码未被浏览器解释为这样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18887292/