REST API with Mongoose
在REST API Server中,每一个来自前端的请求都要进行相应的后端数据库操作。
例如,响应GET
请求要在数据库进行数据检索:为了端到端地处理GET
请求,一个来自客户端的GET
请求,意味着客户端想要在服务端检索数据来使用;所以,要经过多个Express 服务的处理,然后业务逻辑需要在数据库中执行一个查询操作;启动一个查询操作,从数据库获取一组documents,再转换为回复信息,发回给服务器。
所以,这个端到端的请求响应处理涉及到两个部分,一部分是处理业务逻辑,一部分是与数据库进行交互。
再来看POST
请求:
? POST
请求通过请求体发送信息,请求体经Express Server处理后,启动相应的create
请求到MongoDB,在create
请求中,将在数据库中某个collection
中创建新的document
,并将处理后的请求体信息存储到其中。最后,这个操作到的结果通过响应发回给客户端。
所以,对REST API端执行的任何操作,无论是GET、PUT、POST还是DELETE,都会在背后启动对应的数据库操作。也就是说,在REST API端收到的HTTP请求必须映射到相应的数据库操作。
更改dishRouter.js
const express = require('express')
const bodyParser = require('body-parser')
const mongoose = require('mongoose')
const Dishes = require('../models/dishes')
const dishRouter = express.Router()
dishRouter.use(bodyParser.json())
dishRouter.route('/')
.get((req, res, next) => {
Dishes.find({})
.then((dishes) => {
res.statusCode = 200
res.setHeader('Content-Type', 'application/json')
res.json(dishes)
}, (err) => next(err))
.catch((err) => next(err))
})
.post((req, res, next) => {
Dishes.create(req.body)
.then((dish) => {
console.log('Dish Created ', dish)
res.statusCode = 200
res.setHeader('Content-Type', 'application/json')
res.json(dish)
}, (err) => next(err))
.catch((err) => next(err))
})
.put((req, res, next) => {
res.statusCode = 403
res.end('PUT operation not supported on /dishes')
})
.delete((req, res, next) => {
Dishes.reomve({})
.then((resp) => {
res.statusCode = 200
res.setHeader('Content-Type', 'application/json')
res.json(resp)
}, (err) => next(err))
.catch((err) => next(err))
})
dishRouter.route('/:dishId')
.get((req, res, next) => {
Dishes.findById(req.params.dishId)
.then((dish) => {
res.statusCode = 200
res.setHeader('Content-Type', 'application/json')
res.json(dish)
}, (err) => next(err))
.catch((err) => next(err))
})
.post((req, res, next) => {
res.statusCode = 403
res.end('POST operation not supported on /dishes/' + req.params.dishId)
})
.put((req, res, next) => {
res.statusCode = 200
Dishes.findByIdAndUpdate(req.params.dishId, {
$set: req.body
}, {
new: true // 返回改变后的document
})
.then((dish) => {
res.statusCode = 200
res.setHeader('Content-Type', 'application/json')
res.json(dish)
}, (err) => next(err))
.catch((err) => next(err))
})
.delete((req, res, next) => {
Dishes.findByIdAndRemove(req.params.dishId)
.then((resp) => {
res.statusCode = 200
res.setHeader('Content-Type', 'application/json')
res.json(resp)
}, (err) => next(err))
.catch((err) => next(err))
})
dishRouter.route('/:dishId/comments')
.get((req, res, next) => {
Dishes.findById(req.params.dishId)
.then((dish) => {
if (dish != null) {
res.statusCode = 200
res.setHeader('Content-Type', 'application/json')
res.json(dish.comments)
} else {
err = new Error('Dish' + req.params.dishId + ' not found')
err.status = 404
return next(err)
}
}, (err) => next(err))
.catch((err) => next(err))
})
.post((req, res, next) => {
Dishes.findById(req.params.dishId)
.then((dish) => {
if (dish != null) {
dish.comments.push(req.body)
dish.save()
.then((dish) => {
res.statusCode = 200
res.setHeader('Content-Type', 'application/json')
res.json(dish)
}, (err) => next(err))
} else {
err = new Error('Dish ' + req.params.dishId + ' not found')
err.status = 404
return next(err)
}
}, (err) => next(err))
.catch((err) => next(err))
})
.put((req, res, next) => {
res.statusCode = 403
res.end('PUT operation not supported on /dishes/' + req.params.dishId + '/comments')
})
.delete((erq, res next) => {
Dishes.findById(req.params.dishId)
.then((dish) => {
if (dish != null) {
for (let i = (dish.comments.length - 1); i >= 0; i--) {
dish.comments.id(dish.comments[i]._id).remove()
}
dish.save()
.then((dish) => {
res.statusCode = 200
res.setHeader('Content-Type', 'application/json')
res.json(dish)
}, (err) => next(err))
} else {
err = new Error('Dish ' + req.params.dishId + ' not found')
err.status = 404
return next(err)
}
}, (err) => next(err))
.catch((err) => next(err))
})
dishRouter.route('/:dishId/comments/:commentId')
.get((req, res, next) => {
Dishes.findById(req.params.dishId)
.then((dish) => {
if (dish != null && dish.comments.id(req.params.commentId) != null) {
res.statusCode = 200
res.setHeader('Content-Type', 'application/json')
res.json(dish.comments.id(req.params.commentId))
} else if(dish == null) {
err = new Error('Dish ' + req.params.dishId + ' not found')
err.statusCode = 404
return next(err)
} else {
err = new Error('Comment ' + req.params.commentId + ' not found')
res.statusCode = 404
return next(err)
}
}, (err) => next(err))
.catch((err) => next(err))
})
.post((req, res, next) => {
res.statusCode = 403
res.end('POST operation not supported on /dishes/' + req.params.dishId + '/comments/' + req.params.commentId)
})
.put((req, res, next) => {
Dishes.findById(req.params.dishId)
.then((dish) => {
if (dish != null && dish.comments.id(req.params.commentId) != null) {
if (req.body.rating) {
dish.comments.id(req.params.commentId).rating = req.body.rating
}
if (req.body.comment) {
dish.comments.id(req.params.commentId).comment = req.body.comment
}
dish.save()
.then((dish) => {
res.statusCode = 200
res.setHeader('Content-Type', 'application/json')
res.json(dish)
}, (err) => next(err))
} else if (dish == null) {
err = new Error('Dish ' + req.params.commentId + ' not found')
err.statusCode = 404
return next(err)
} else {
err = new Error('Comment ' + req.params.commentId + ' not found');
err.status = 404;
return next(err);
}
}, (err) => next(err))
.catch((err) => next(err));
})
.delete((req, res, next) => {
Dishes.findById(req.params.dishId)
.then((dish) => {
if (dish != null && dish.comments.id(req.params.commentId) != null) {
dish.comments.id(req.params.commentId).remove()
dish.save()
.then((dish) => {
res.statusCode = 200
res.setHeader('Content-Type', 'application/json');
res.json(dish)
}, (err) => next(err))
} else if (dish == null) {
err = new Error('Dish ' + req.params.dishId + ' not found');
err.status = 404
return next(err)
} else {
err = new Error('Comment ' + req.params.commentId + ' not found');
err.status = 404
return next(err)
}
}, (err) => next(err))
.catch((err) => next(err))
})
module.exports = dishRouter