当我在node.js中使用以下代码加载localhost:3000时,网页没有收到任何信息。这是为什么?

问题描述 投票:0回答:1
const http = require('http');

const server = http.createServer((req, res) => {
console.log(req.url, req.method, req.headers);
//process.exit();
res.setHeader('Content-Type', 'test/html');
res.write('<html>');
res.write('<head><title>My first page</title></head>');
res.write('<body><h1>Hello from my Node.js Server</h1></body>');
res.write('</html>');
res.end();

});

server.listen(3000);

我期待页面从我的 Node.js 中打招呼,但什么也没收到

html node.js
1个回答
0
投票

因为内容类型无效,

'Content-Type', 'test/html'

应该是

'Content-Type', 'text/html'

可以在此处找到有效的内容类型https://www.iana.org/assignments/media-types/media-types.xhtml

© www.soinside.com 2019 - 2024. All rights reserved.