Interface development based on Express framework

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

var classSchema = new Schema({
"classList":[
{
"logo": String,
"title": String,
"detail": String,
"price": String
}
]
});

// 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

var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var Class = require('../models/class')

//Connect to MongoDB database
// mongoose.connect('mongodb://root:[email protected]:27017/wxapp') //If there is an account password, writing root is the account 123456 is the password
mongoose.connect('mongodb://127.0.0.1:27017/wxapp') // 127.0.0.1:27017 is the database address, wxapp is the database name

 

 
//connection succeeded
mongoose.connection.on("connected", function () {
console.log("MongoDB connected success.")
})
 
//Connection failed
mongoose.connection.on("error", function () {
console.log("MongoDB connected faile.")
})
 
//connection interrupted
mongoose.connection.on("disconnected", function () {
console.log("MongoDB connected disconnected.")
})
 
//getClassList is a secondary route,
router.get('/getClassList', function (req, res, next) {
Classe.find({}, function (err, doc) {
if (err) {
res.json({
status: '1',
msg: err.message
})
} else {
res.json({
status: '0',
msg: '',
result: {
count: doc.length,
list: doc
}
})
}
})
});

module.exports = router;

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)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324611731&siteId=291194637