1.HTTP状态码
200
请求成功404
请求的资源没有被找到500
服务器端错误400
客户端请求有语法错误
2.内容类型
- text/html
- text/css
- application/javascript
- image/jpeg
- application/json
writeHead状态码里面第一个参数是状态码,第二个参数是一个对象,这个对象其实就是响应头里面的信息,响应报文信息。’content-type‘
可以根据当前返回的内容来设置它,如果当前返回的内容是纯文本这个时候指定的值是text/plain.
res.writeHead(200,{
'content-type':'text/html;charset = utf8'
})
// 用于创建网站服务器的模块
const http = require('http');
// app对象就是网站服务器对象
const app = http.createServer();
// 当用户端有请求来的时候
// req请求对象,包含了请求相关的信息,
// 获取请求方式
app.on('request',(req,res) => {
// res.end 结束请求并且为客户端响应内容
// console.log(req.method);
// console.log(req.url);
// console.log(req.headers['accept']);
// 书写响应报文
res.writeHead(200,{
'content-type':'text/html;charset = utf8'
})
if(req.url == '/index' || req.url == '/'){
res.end('<h2>welcome to homepage</h2>');
}else if(req.url == '/list'){
res.end('<h2>Welcome to listpage</h2>');
}else{
res.end('<h2>not found</h2>');
}
if (req.method == 'POST'){
res.end('POST')
}else if(req.method == 'GET'){
res.end('get')
}
// res.end('<h2>hello user</h2>');
});
// 监听端口
app.listen(3000);
console.log("网站服务器启动成功")