REST API with Mongoose
In the REST API Server, a request from each of the front end have corresponding backend database operations.
For example, in response to GET
a request for a data search in the database: In order to end the processing GET
request from a client's GET
request, means that the client tries to access the server to retrieve data; therefore, to be processed by a number Express service, then you need to perform the business logic in the database query operation; start a query, retrieve a set of documents from the database, and then converted to the reply message is sent back to the server.
Therefore, the process end request response involves two parts, one business logic, part interact with the database.
Look at POST
request:
POST
Request information by requesting, somatic Express Server after treatment, a corresponding request start create
request to MongoDB, the create
request will be in a database collection
to create a new document
, and stores the request body the information processed therein. Finally, the results of the operation to the response back to the client.
So, for any operation REST API end execution, either GET, PUT, POST or DELETE, will start the corresponding database operations in the background. That is, HTTP client receives the REST API requests must be mapped to a corresponding database operations.
changedishRouter.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