# 服务端基础概念
* URL 统一资源定位符 网址就是url
* url 组成 传输协议://服务器ip地址或域名:端口/资源所在位置标识
## 创建web 服务器
* 需要访问 服务器 需要 在命令行输入 nodemon + 文件名称
* 输入 localhost:端口名
```
//引入系统模块
const http = require('http');
//创建 web 服务器 app对象就是网站服务对象
const app = http.createServer();
//当客户端有请求来的时候
app.on('request',(req,res)=>{
// 获取请求方式
console.log(req.method);
//响应
res.end('<h1>hi,user</h1>')
});
//监听端口
app.listen(3000)
console.log('3000,端口已启动');
```
## http 协议
1. http协议概念
* 超文本传输协议(htyper text transfer protocol 缩写 http)规定了如何从网站服务器传输超文本到本地浏览器 ,它基于客户端服务器架构工作,是客户(用户)和服务器端(网站)请求和应答的标准
1. 报文: 在http请求 和响应的过程中传递的数据快就是报文,包括要传送的数据和一些附加信息,并且要遵守规定好的格式
* 请求报文 : 有客户端想服务器端发送的请求就是请求报文
* GET 发送请求
* POST 发送数据
```
app.on('request',(req,res)=>{
// 获取请求方式
//根据请求方式 分别响应 响应内容
//console.log(req.method);
if(req.method == 'GET'){
res.end('<h1>GET</h1>')
}else if(req.method == 'post'){
res.end('<h1>POST</h1>')
}
//响应
//res.end('<h1>hi,user</h1>')
});
```
```
//引入系统模块
const http = require('http');
//创建 web 服务器 app对象就是网站服务对象
const app = http.createServer();
//当客户端有请求来的时候
//console.log(req.url);
app.on('request',(req,res)=>{
// 获取请求方式 req.method
//获取请求报文 raq.headers
// req.url 获取请求地址
console.log(raq.headers['accept']);
//console.log(req.url);
//根据请求地址 做出不同的响应
// if(req.url === './index'){
// res.end('<h1>POST1</h1>')
// }else if(req.url === './list'){
// res.end('<h1>POST0</h1>')
// }else{
// res.end('<h1>POST2</h1>')
// }
//根据请求方式 分别响应 响应内容
if(req.method == 'GET'){
res.end('<h1>GET</h1>');
}else if(req.method == 'post'){
res.end('<h1>POST</h1>');
}
//响应
//res.end('<h1>hi,user</h1>')
});
//监听端口
app.listen(3000);
console.log('3000,端口已启动');
```
* 响应报文 :由服务器接受到请求报文,后给到客户端的信息就是 响应报文
1. http状态码
* 200 请求成功
* 404 请求的资源没有找到
* 500 服务器端错误
* 400 客户端请求有语法错误
2. 内容类型
* // res.writeHead(200,{
* // " content-type":"text/html;charset=utf8"
* // });
3. 请求参数
* 参数被放置在浏览器地址栏中。例如 localhost:3000/index?name=red&age=20
4. 路由
* 路由是指客户端请求地址与服务器端程序代码的对应关系
5. 动态资源
* 相同请求地址,不同的响应资源,这种资源称为动态资源