Start by defining a Model that points to the database template to search for the corresponding data.
const Models = require(`.. /.. /models/${modelName}`)Copy the code
ModelName is used to retrieve the passed sub-address from the address via req.params.resource
const modelName = require('inflection').classify(req.params.resource)
Copy the code
(Note: Inflection here is a plugin I’ve introduced to turn plural categories into singular categories with uppercase letters, because the template name I defined was Category.)
Examples are as follows
app.use('/admin/api/rest/:resource', router)
Copy the code
We pass in resource in this way, assuming the front end opens the classification page, so here is the route name we define, and we modify the corresponding data of the corresponding page in this way.
Resource first passes the first function to a valid template path and then responds to the child router via req.models and other data
Using Models, you can go to the database to find the data of the corresponding template, which realizes the generalization of THE CRUD interface
Note: in order to deepen the impression and understanding of the writing of the passage, if you have any questions welcome to point out! thank you