注意三种查询参数的区别

  • 路由映射参数
    • 从req.params中获取
  • get方法,路由拼接参数
    • 从req.query中获取
  • post/put方法,body中传递
    • 从req.body中获取

查询接口

查询列表

创建routes/admin/webs.js路由映射文件,注意:

  • await/async异步处理
  • condition条件查询
    • order 控制排序
    • where 条件查询
  • try/catch异常捕获
  • 返回值格式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const express = require('express');
const router = express.Router();
const {Op} = require("sequelize");
const {Web} = require('../../models')

router.get('/',async (req,res)=>{
try{
const condition = {
// 添加查询排序条件:根据id从大到小排序
order:[
['id','desc']
],
where:{
name:{
[Op.like]:`%${name}%`
}
}
}
const webs = await Web.findAll(condition)
res.json({
status:true,
message:'站点列表查询成功',
data:{
webs
}
})
}catch(error){
// 异常处理
res.status(500).json({
status:false,
message:"查询站点列表失败。",
errors:[error.message]
})
}

})

module.exports = router;

在app.js中引入,并进行路径映射:

1
2
const adminWebsRouter = require('./routes/admin/webs')
app.use('/admin/webs', adminWebsRouter);

查询http://localhost:3000/admin/webs
即可查询出数据

查询详情

查询详情接口需要注意:

  • 从request.params中获取id
  • 注意处理id未找到的情况
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
* 查询站点详情
*/
router.get('/:id',async (req,res)=>{
try{
const {id} = req.params;
const web = await Web.findByPk(id)
if(web){
res.json({
status:true,
message:'站点详情查询成功',
data:web
})
}else{
res.status(404).json({
status:false,
message:'站点信息未找到'
})
}
}catch(error){
// 异常处理
res.status(500).json({
status:false,
message:"查询站点详情失败。",
errors:[error.message]
})
}
})

新增接口

新增

新增接口注意事项:

  • 创建了新的资源,返回状态码201
  • 使用create创建新的数据
  • 对req.params中的数据进行白名单筛选
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* 新建站点
*/
router.post('/',async (req,res)=>{
try{
const body = getFilterBody(req)
const web = await Web.create(body)
res.status(201).json({
status:true,
message:'创建站点成功',
data:web
})
}catch(error){
// 异常处理
res.status(500).json({
status:false,
message:"创建站点失败",
errors:[error.message]
})
}

})

/**
* 获取到过滤后的body参数
* @param request
*/
function getFilterBody(req){
const {
href,
name,
webIcon,
webCover,
content,
categoryId,
view,
github,
blogHref
} = req.body
return{
href,
name,
webIcon,
webCover,
content,
categoryId:Number(categoryId),
view:view?Number(view):0,
github,
blogHref
}
}

删除接口

删除注意事项

  • 使用destroy方法
  • 删除之前先寻找对应id数据是否存在
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
* 删除站点
*/
router.delete('/:id',async (req,res)=>{
try{
const {id} = req.params;
const web = await Web.findByPk(id)
if(web){
await web.destroy()
res.json({
status:true,
message:'删除站点成功'
})
}else{
res.status(404).json({
status:false,
message:'未找到指定站点'
})
}
}catch(error){
// 异常处理
res.status(500).json({
status:false,
message:"删除站点失败",
errors:[error.message]
})
}
})

更新接口

更新接口注意

  • 使用update方法进行更新
  • 先根据id查询数据是否存在,再进行更新
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* 更新站点
*/
router.put('/:id',async (req,res)=>{
try{
const {id} = req.params;
const web = await Web.findByPk(id)
if(web){
const body = getFilterBody(req)
await web.update(body)
res.json({
status:true,
message:'站点更新成功',
data:web
})
}else{
res.status(404).json({
status:false,
message:'站点未找到'
})
}
}catch(error){
// 异常处理
res.status(500).json({
status:false,
message:"站点更新失败。",
errors:[error.message]
})
}
})

模糊搜索

模糊搜索

模糊搜索使用的sql语句:

1
select * from Articles where title like '%标题 10%'`

sequelize中,想要使用模糊搜索,需要用到Op:

1
const { Op } = require('sequelize');

在condition中添加where属性:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* 查询站点列表
*/
router.get('/',async (req,res)=>{
try{
const {name} = req.query
const condition = {
// 添加查询排序条件:根据id从大到小排序
order:[
['id','desc']
]
}
if(name){
condition.where = {
name:{
[Op.like]:`%${name}%`
}
}
}
const webs = await Web.findAll(condition)
// .....
}catch(error){
// .....
}

})

分页查询

分页查询

分页查询的sql语句:

1
SELECT * FROM `Articles` LIMIT offset, limit;

分页查询注意事项:

  • 根据currentPage和pageSize,手动计算offset
  • 使用findAndCount方法
  • condition条件中加入offset和limit属性
  • 从返回值中手动提取rows和count值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
router.get('/',async (req,res)=>{
try{
const {name} = req.query
const currentPage = Math.abs(Number(req.query.currentPage || 1))
const pageSize = Math.abs(Number(req.query.pageSize || 10))
const offset = (currentPage - 1) * pageSize
const condition = {
// 添加查询排序条件:根据id从大到小排序
order:[
['id','desc']
],
limit:pageSize,
offset
}
if(name){
condition.where = {
name:{
[Op.like]:`%${name}%`
}
}
}
// const webs = await Web.findAll(condition)
const {rows, count} = await Web.findAndCountAll(condition)
res.json({
status:true,
message:'站点列表查询成功',
data:{
webs:rows,
pagination:{
total:count,
currentPage,
pageSize
}
}
})
}catch(error){
// 异常处理
res.status(500).json({
status:false,
message:"查询站点列表失败。",
errors:[error.message]
})
}
})