NodeJs Review:使用express写一个get接口

背景

get请求在我们的工作当中是经常会使用到的一种请求数据方式,为了更好地理清get请求,今天便利用express写出一个get的接口,话不多说,上代码~~

1.创建服务器三件套

利用express快速创建出一个服务器

//1.导入模块
const express = require('express')

//2.创建服务器
let app = express()

//3.开启服务器
app.listen(80, () => {
	console.log('success')
}) 

2.创建模拟数据

const allData = [
	{
		"id": 1,
		"bookname": "西游记",
		"author": "吴承恩",
		"publisher": "北京图书出版社"
	},
	{
		"id": 2,
		"bookname": "红楼梦",
		"author": "曹雪芹",
		"publisher": "上海图书出版社"
	},
	{
		"id": 3,
		"bookname": "三国演义",
		"author": "罗贯中",
		"publisher": "北京图书出版社"
	}
] 

3.定义路由

app.get('/api/getbooks', (req, res) => {} 

4.为用户可能传递的参数进行结构赋值

const { id, bookname, author, publisher } = req.query 

5.判断用户是否传参或传递了何种参数,从而返回数据

let data = []
if (id) {
 data = allData.filter(item => item.id == id)
 let newData = {
		"status": 200,
		"msg": "获取图书列表成功",
		"data": data
	}
	res.send(newData)
} else if (bookname) {
 data = allData.filter(item => item.bookname == bookname)
 let newData = {
		"status": 200,
		"msg": "获取图书列表成功",
		"data": data
	}
	res.send(newData)
} else if (author) {
 data = allData.filter(item => item.author == author)
 let newData = {
		"status": 200,
		"msg": "获取图书列表成功",
		"data": data
	}
	res.send(newData)
} else if (publisher) {
 data = allData.filter(item => item.publisher == publisher)
 let newData = {
		"status": 200,
		"msg": "获取图书列表成功",
		"data": data
	}
	res.send(newData)
} else {
	let newData = {
			"status": 200,
			"msg": "获取图书列表成功",
			"data": allData
		}
		res.send(newData)
	} 

6.代码优化

上一步便已经完成了写get接口的任务,但在判断这一块代码显得冗余,非常不利于阅读,为此进行了优化

if (id) {
		data = allData.filter(item => item.id == id)
	} else if (bookname) {
		data = allData.filter(item => item.bookname == bookname)
	} else if (author) {
		data = allData.filter(item => item.author == author)
	} else if (publisher) {
		data = allData.filter(item => item.publisher == publisher)
	} else {
		let newData = {
			"status": 200,
			"msg": "获取图书列表成功",
			"data": allData
		}
		res.send(newData)
		return
	}
	let newData = {
		"status": 200,
		"msg": "获取图书列表成功",
		"data": data
	}
	res.send(newData)
}) 

完整代码

//1.导入模块
const express = require('express')
//2.创建服务器
let app = express()

const allData = [
	{
		"id": 1,
		"bookname": "西游记",
		"author": "吴承恩",
		"publisher": "北京图书出版社"
	},
	{
		"id": 2,
		"bookname": "红楼梦",
		"author": "曹雪芹",
		"publisher": "上海图书出版社"
	},
	{
		"id": 3,
		"bookname": "三国演义",
		"author": "罗贯中",
		"publisher": "北京图书出版社"
	}
]
app.get('/api/getbooks', (req, res) => {
	let data = []
	const { id, bookname, author, publisher } = req.query
	if (id) {
		data = allData.filter(item => item.id == id)
	} else if (bookname) {
		data = allData.filter(item => item.bookname == bookname)
	} else if (author) {
		data = allData.filter(item => item.author == author)
	} else if (publisher) {
		data = allData.filter(item => item.publisher == publisher)
	} else {
		let newData = {
			"status": 200,
			"msg": "获取图书列表成功",
			"data": allData
		}
		res.send(newData)
		return
	}
	let newData = {
		"status": 200,
		"msg": "获取图书列表成功",
		"data": data
	}
	res.send(newData)
})

//3.开启服务器
app.listen(80, () => {
	console.log('success')
}) 

tips:写完记得跑下服务器再测试哦!!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值