const http=require("http");
const server=http.createServer();
//req是请求对象,包含了与客户端相关的属性和数据
server.on('request',function(req,res){
const url =req.url;
//req.method是客户端请求的method类型
const method =req.method;
const str=`您请求的url地址是 ${url},请求的method类型是 ${method}`;
console.log(str);
//调用res.end向客户端响应内容
res.end(str);
}
)
server.listen(80,function(){
console.log('server running at http://127.0.0.1')
})
通过res.end服务器响应请求的的时候出现了中文乱码
解决办法:手动设置编码格式---设置响应头Content-Type的值为text/html; charset=utf-8
res.setHeader('Content-Type','text/html; charset=utf-8');
const http=require("http");
const server=http.createServer();
//req是请求对象,包含了与客户端相关的属性和数据
server.on('request',function(req,res){
const url =req.url;
//req.method是客户端请求的method类型
const method =req.method;
const str=`您请求的url地址是 ${url},请求的method类型是 ${method}`;
console.log(str);
//设置响应头Content-Type的值为text/html; charset=utf-8
res.setHeader('Content-Type','text/html; charset=utf-8');
//调用res.end向客户端响应内容
res.end(str);
}
)
server.listen(80,function(){
console.log('server running at http://127.0.0.1')
})