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

596 阅读2分钟

背景

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:写完记得跑下服务器再测试哦!!