1.response.writeHeader():告诉浏览器以何种方式解析内容,并且该方法在消息中只能调用一次。
let http = require('http');
http.createServer(function(req,res){
res.writeHeader(200,{'Content-Type':'text/html'});
res.write('<h1>hello<h1>');
res.end();
}).listen(8080,'127.0.0.1');
console.log('start the sever');res.writeHeader(200,{'Content-Type':'text/html'});告诉浏览器,将接收到的数据<h1>hello<h1>按照html格式解析内容let http = require('http');
http.createServer(function(req,res){
res.writeHeader(200,{'Content-Type':'text/plain'});
res.write('<h1>hello<h1>');
res.end();
}).listen(8080,'127.0.0.1');
console.log('start the sever');
当不写response.writeHeader()时,会根据不同内容进行解析
内容是字符串'<h1>hello<h1>'时,按照text/html解析,内容是字符串'hello<h1>'时,按照text/plain解析:
let http = require('http');
http.createServer(function(req,res){
res.write('<h1>hello<h1>');
res.end();
}).listen(8080,'127.0.0.1');
console.log('start the sever');网页显示内容:hellolet http = require('http');
http.createServer(function(req,res){
res.write('hello<h1>');
res.end();
}).listen(8080,'127.0.0.1');
console.log('start the sever');网页显示内容:hello<h1>
本文介绍了如何使用response.writeHeader()设置HTTP响应头来控制浏览器解析内容的方式,并通过示例展示了不同Content-Type设置对页面显示效果的影响。

1382

被折叠的 条评论
为什么被折叠?



