. 在
静态资源服务器 的基础上,监听用户的请求,如:GET、POST、PATCH、DELETE等
服务器需求
- 所有的数据处理在 JSON 文件中完成,需要文件系统 fs 库
const fs = require('fs')
处理提交的 JSON 数据,需要 json() 库
app.use(express.json())
仅开放用户和商品接口,其它接口如留言、代办事项、问卷等请参照设计
商品 goods
// 读取JSON数据
const data = fs.readFileSync("./public/data/coffee.json");
const coffee = JSON.parse(data).cont;
1. 全部拉取 - GET
app.get('/goods', (req, res) => {
res.json({
"errno": 0,
"msg": 'goods loading ok',
"data": coffee
})
})
优化后,还可以根据 id 获取数据
app.get('/goods', (req, res) => {
const id = req.query.id;
if (id) {
res.json({
"errno": 0,
"msg": 'good loading ok',
data: coffee.find((item) => item.id == id),
})
}else{
res.json({
"errno": 0,
"msg": 'goods loading ok',
"data": coffee
})
}
})
2. 分页拉取 - GET
适合数据较多的时候
请求格式 /goods/page?page=x&pageSize=y
app.get('/goods/page', (req, res) => {
console.log('page', req.query);
let arr = [...jsonFile.cont]
let start = (req.query.page - 1) * req.query.pageSize;
let result = arr.splice(start, req.query.pageSize)
res.setHeader('X-Total-Count', jsonFile.cont.length);
res.json({
"errno": 0,
"msg": 'goods page loading ok',
"data": result
});
});
用户 users
. 无须准备数据文件
获取用户
- GET
- 用户修改信息时使用
- 用户提交学号 usn
- 获取并解析对应的.json 文件,返回给用户
app.get('/user', (req, res) => {
console.log(req.query);
let file = fs.readFileSync('./public/data/' + req.query.usn + '.json', 'utf8')
let jsonFile = JSON.parse(file)
console.log(jsonFile);
res.json({
"errno": 0,
"msg": 'get ok',
"data": jsonFile
})
})
用户登录
- GET
- 用户提交学号 usn 、密码 upass
- 读取对应学号的.json 文件;如果不存在,提示用户不存在,需要登录
- 如果文件存在,则解析文件,获取密码,和提交的密码比较,相等,登录成功;否则登录失败
app.get('/users/login', (req, res) => {
console.log('req.query', req.query);
let file = fs.readFile('./public/data/' + req.query.usn + '.json', 'utf8', (err, data) => {
if (err) {
res.json({
"errno": 2,
"msg": 'user not exist'
})
} else {
let jsonFile = JSON.parse(data)
if (req.query.upass === jsonFile.upass) {
res.json({
"errno": 0,
"msg": 'login ok',
"data": jsonFile
})
} else {
res.json({
"errno": 1,
"msg": 'pass not match'
})
}
}
})
})
用户注册
- POST
- 用户提交学号 usn、姓名 uname 和密码 upass
- 检查对应学号的.json 文件是否存在;如果存在,则提示用户已存在,不允许注册
- 以学号为文件名创建用户文件,保存用户信息;如果成功则提示用户注册成功;否则提示用户稍后再试
app.post('/users/register', (req, res) => {
console.log('req.body', req.body);
if (fs.existsSync('./public/data/' + req.body.usn + '.json')) {
res.json({
"errno": 2,
"msg": 'user exist'
})
return;
}
fs.writeFile('./public/data/' + req.body.usn + '.json', JSON.stringify(req.body), (err) => {
if (err) {
console.log('保存文件失败')
res.json({
"errno": 1,
"msg": 'try later'
})
} else {
console.log('保存文件成功')
res.json({
"errno": 0,
"msg": 'register ok'
})
}
})
})
修改用户信息
- POST
- 按照用户信息字段修改
- 用户提交要修改的字段 ukey 和对应的值 uvalue
- 读取、解析、更新、覆写,完成用户信息的修改
app.post('/users/modify', (req, res) => {
console.log('modify', req.body);
const data = JSON.parse(fs.readFileSync('./public/data/' + req.body.usn + '.json', 'utf8'));
data[req.body.ukey] = req.body.uvalue
fs.writeFile('./public/data/' + req.body.usn + '.json', JSON.stringify(data, null, 4), (error) => {
if (error) {
console.log('保存文件失败')
res.json({
"errno": 2,
"msg": 'try later'
})
} else {
console.log('保存文件成功')
res.json({
"errno": 0,
"msg": 'modify ok'
})
}
})
})
头像上传
- 用户上传头像,保存到服务器,并返回保存后的头像地址
- 详细操作,请查看 头像上传 - multer
- 这里按照学号 usn 命名图片,相应的存储配置如下
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, __dirname + '/public/upload')
},
filename: (req, file, cb) => {
let ext = path.extname(file.originalname)
cb(null, req.body.usn + ext)
}
})
上传头像 - 提供学号 usn 和头像 avatar
app.post('/avatar', upload.single('avatar'), (req, res) => {
console.log('req.file', req.file);
console.log('req.body', req.body);
let file = fs.readFileSync('./public/data/' + req.body.usn + '.json', 'utf8')
let jsonFile = JSON.parse(file);
jsonFile.imgUrl = 'http://127.0.0.1:3000/upload/' + req.file.filename
file = fs.writeFileSync('./public/data/' + req.body.usn + '.json', JSON.stringify(jsonFile, null, 4))
res.json({
"errno": 0,
"msg": "upload ok",
"data": {
"url": 'http://127.0.0.1:3000/upload/' + req.file.filename,
}
})
})