我已经创建了我的第一个节点js应用程序:一个简单的have服务器。下面是代码:
// 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代码?
发布于 2013-09-19 06:17:35
您已经明确表示,您返回的是纯文本,而不是HTML。因此,浏览器将其视为纯文本。
如果您希望HTML被视为HTML,那么就说它是HTML:
{"Content-Type": "text/html"}(尽管您应该返回一个HTML文档,而不是HTML的片段)。
发布于 2013-09-19 06:21:40
以下守则适用于我:
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/html"});
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/");你需要设置标题。有关更多信息,请在节点API文档在这里。中检查
检查firebug或dev工具中的不同之处,以了解浏览器如何根据标头内容类型进行不同的解释。
https://stackoverflow.com/questions/18887292
复制相似问题