There are four main steps to develop an interface based on the Express framework:
1. Install Mongoose
Through the introduction of the previous article " Building an Operating Environment Based on Express Framework ", after creating the service, install mongoose through npm , command cnpm i mongoose -g (requires installation of Taobao mirror)
2. Create a model
Create a new model folder to store the model
The basic composition of the model (directly paste the code):
var mongoose = require('mongoose')
var Schema = mongoose.Schema // used to define the table model
//create model
// output model
module.exports = mongoose.model('Kecheng',classSchema )
// It should be noted here that the name of the output model should be related to the name of the database table you want to associate. For example, if the model name is Kecheng , the name of the database table you want to associate should be set to Kecheng s ; if there is no corresponding s, you can Add a third parameter to specify the table that needs to be associated. Such as
// module.exports = mongoose.model('Kecheng',classSchema ,'otherCollectionName')
3. Create a route
Set the first-level routing in app.js, see the figure below
this is the directory
4. Based on mongoose, realize interface function
After setting the first-level route, you can write the interface under the routes folder
I created a classe.js below routes with the following code
At this point, the interface is written, and the interface can be called after starting the service.
It is recommended to use pm2 to start the node service, and it can be used by npm installation. The startup command is pm2 start bin/www, the shutdown command is pm2 stop bin/www, and the command to close all services is pm2 stop all
After the startup is successful, open it in the browser and you can see it
The JSON-handle plugin is used here, which can format JOSN data
·
·
·
·
·
That's all for now, hope that helps.
If you have any questions, please contact QQ 412606846 (same number on WeChat)