web服务器
当应用程序(客户端)需要某一个资源时,可以向一个台服务器,通过Http请求获取到这个资源;提供资源的这个服务器,就是一个Web服务器;
初体验
// 引入 http 模块
const http = require('http')
// 创建一个web服务器
const server = http.createServer((req, res) => {
res.end('Hello Http')
})
// 开启服务器并且监听端口
server.listen(8000, 'localhost' ,() => {
console.log('服务器启动成功');
})
request
- req.url:本次请求的URL,服务器需要根据不同的URL进行不同的处理;
- req.method:本次请求的请求方式,比如GET、POST请求传入的参数和处理的方式是不同的;
- req.headers:本次请求的headers中也会携带一些信息,比如客户端信息、接受数据的格式、支持的编码格式等;
req.url
利用 postman 发送请求
url传输数据:localhost:8000/users?username=htf&password=123456
基本使用方式
根据不同请求路径返回不同内容
const http = require('http')
// 创建一个web服务器
const server = http.createServer((req, res) => {
if(req.url == '/login') {
res.end('欢迎回来');
} else if(req.url == '/user') {
res.end('欢迎登录用户列表');
} else {
res.end('不存在该页面');
}
})
// 开启服务器并且监听端口
server.listen(8000, () => {
console.log('服务器启动成功');
})
url模块
发送请求的时候,可借助url模块解析
const http = require('http')
const url = require('url')
// 创建一个web服务器
const server = http.createServer((req, res) => {
const parseInfo = url.parse(req.url)
console.log(parseInfo);
res.end('发送成功')
})
// 开启服务器并且监听端口
server.listen(8000, () => {
console.log('服务器启动成功');
})
此时控制台输出解析成功后的对象
Url {
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: '?username=htf&password=123456',
query: 'username=htf&password=123456',
pathname: '/users',
path: '/users?username=htf&password=123456',
href: '/users?username=htf&password=123456'
}
我们可拿到 query 与 pathname
const http = require('http')
const url = require('url')
// 创建一个web服务器
const server = http.createServer((req, res) => {
const { pathname, query } = url.parse(req.url)
console.log(pathname, query);
res.end('发送成功')
})
// 开启服务器并且监听端口
server.listen(8000, () => {
console.log('服务器启动成功');
})
输出结果:/users username=htf&password=123456
可借用另一模块 对 query 进行解析
querystring
此时控制台输出( htf 123456) 对query解析成功
const http = require('http')
const url = require('url')
const qs = require('querystring')
// 创建一个web服务器
const server = http.createServer((req, res) => {
const { pathname, query } = url.parse(req.url)
const queryObj = qs.parse(query)
console.log(queryObj.username);
console.log(queryObj.password);
res.end('发送成功')
})
// 开启服务器并且监听端口
server.listen(8000, () => {
console.log('服务器启动成功');
})
methods
{
“name”: “htf”,
“password”: “123456”
}
以上的 body 内的 json 数据 通过 post 方式发送请求
const http = require("http");
const url = require("url");
const qs = require("querystring");
// 创建一个web服务器
const server = http.createServer((req, res) => {
const { pathname } = url.parse(req.url);
// 可判断请求方式
if (req.method === "POST") {
if (pathname == "/login") {
// 拿到 body 中的数据
// 指定编码方式 否则返回二进制
req.setEncoding("utf-8");
req.on("data", (data) => {
// 因为发送的 json 数据 以这种方式进行解析
const { name, password } = JSON.parse(data);
console.log(name, password); // htf 123456
});
} else {
res.end("不存在该页面");
console.log("接受失败");
}
}
});
// 开启服务器并且监听端口
server.listen(8000, () => {
console.log("服务器启动成功");
});
在Restful规范(设计风格)中,我们对于数据的增删改查应该通过不同的请求方式
- GET:查询数据;
- POST:新建数据;
- PATCH:更新数据;
- DELETE:删除数据;
headers
在request对象的header中也包含很多信息,客户端会默认传递过来一些信息:
{
'content-type': 'application/json',
'user-agent': 'PostmanRuntime/7.28.1',
accept: '*/*',
'postman-token': 'bf377318-75ca-4a27-b524-6ffcfa9dddad',
host: 'localhost:8000',
'accept-encoding': 'gzip, deflate, br',
connection: 'keep-alive',
'content-length': '50'
}
content-type是这次请求携带的数据的类型
- application/json表示是一个json类型;
- text/plain表示是文本类型;
- application/xml表示是xml类型;
- multipart/form-data表示是上传文件;
user-agent:客户端相关的信息;
accept:告知服务器,客户端可接受文件的格式类型;
accept-encoding:告知服务器,客户端支持的文件压缩格式,比如js文件可以使用gzip编码,对应 .gz文件;
content-length:文件的大小和长度
connection: ‘keep-alive’
- http是基于TCP协议的,但是通常在进行一次请求和响应结束后会立刻中断;
- 在http1.0中,如果想要继续保持连接:
- 浏览器需要在请求头中添加 connection: keep-alive;
- 服务器需要在响应头中添加 connection:keey-alive;
- 当客户端再次放请求时,就会使用同一个连接,直接一方中断连接;
- 在http1.1中,所有连接默认是 connection: keep-alive的;
- 不同的Web服务器会有不同的保持 keep-alive的时间;
- Node中默认是5s
response
状态码
Http状态码(Http Status Code)是用来表示Http响应状态的数字代码
状态代码 | 状态描述 | 说明 |
---|---|---|
200 | OK | 客户端请求成功 |
400 | Bad Request | 由于客户端请求有语法错误,不能被服务器所理解 |
401 | Unauthorized | 请求未经授权。这个状态码必须和WWW-Authenticate报头域一起使用 |
403 | Forbidden | 服务器收到请求,但是拒绝服务。服务器通常会在响应正文中给出不提供服务的原因 |
404 | Not Found | 请求的资源不存在,例如:输入错误的URL |
500 | internal Serve Error | 服务器发生不可预期的错误,导致无法完成客户端的请求 |
503 | Service Unavailable | 服务器当前不能够处理客户端的请求,在一段时间之后,服务器可能会恢复正常 |
设置状态码
const http = require("http");
const url = require("url");
const qs = require("querystring");
// 创建一个web服务器
const server = http.createServer((req, res) => {
// 设置状态码
// 方式一:直接给属性赋值
// res.statusCode = 404
// 方式二:和 head 一起设置
res.writeHead(404)
// 响应结果
res.end('Hello Http')
});
// 开启服务器并且监听端口
server.listen(8000, () => {
console.log("服务器启动成功");
});