1. Configuration
conf.js
module.exports = {
db: {
url: 'mongodb://127.0.0.1:27017/local',
options: {
useNewUrlParser: true,
}
}
}
2. Entrance
index.js
const Koa = require('koa');
const app = new Koa();
const config = require('./conf')
const {loadModel} = require('./framework/loader')
//Load the configuration corresponding field of the model
loadModel(config)(app)
//Introduction of dynamic configuration API
const rest = require('./framework/router');
//Automatically convert the incoming body string to the object value, cxt.request.body
const bodyParser = require('koa-bodyparser');
app.use(bodyParser());
app.use(rest);
const port = 3000;
app.listen(port, () => {
console.log('link 3000');
})
3. Define schema
./model/user.js
module.exports = {
schema: {
mobile: {
type: String,
required: true,
},
realName: {
type: String,
required: true,
},
}
}
4. Load the defined schema file and read the configuration to Mongo
./framework/loader.js
const fs = require('fs');
const path = require('path');
const mongoose = require('mongoose');
function load (dir, cb) {
//Get absolute path
const url = path.resolve(__dirname, dir);
const files = fs.readdirSync(url);
files.forEach(filename => {
filename = filename.replace('.js', '');
const file = require(url + '/' + filename);
cb(filename, file);
})
}
const loadModel = (config) => {
return (app) => {
mongoose.connect(config.db.url, config.db.options);
const conn = mongoose.connection;
conn.on('error', () => {
console.error ('connection failed ')
})
app.$model = {
}
load('../model', (filename, {schema}) => {
console.log('load model '+ filename, schema)
app.$model[filename] = mongoose.model(filename, schema)
})
}
}
module.exports = {
loadModel
}
In the above steps, you can define the data model in Mongo database. Next, you can map the data model to the interface
5. Define interface name
./framework/router.js
const router = require('koa-router')();
const {
init, get, create, update, del,
} = require('./api');
router.get('/api/:listname', init, get);
router.post('/api/:listname', init, create);
router.put('/api/:listname/:id', init, update);
router.delete('/api/:listname/:id', init, del);
module.exports = router.routes();
6. Specific handling of interface
./framework/api.js
module.exports = {
async init (ctx, next) {
console.log('init', ctx.params);
const model = ctx.app.$model[ctx.params.listname];
if (model) {
ctx.listname = model;
await next();
} else {
ctx.body = 'no this model'
}
},
async get (ctx) {
ctx.body = await ctx.listname.find({})
},
async create (ctx) {
const req = await ctx.listname.create(ctx.request.body)
console.log('req', req);
console.log('ctx', ctx.listname);
ctx.body = req;
},
async update (ctx) {
const res = await ctx.listname.updateOne({
_id: ctx.params.id
}, ctx.request.body);
ctx.body = res;
console.log('res', res);
},
async del (ctx) {
const res = await ctx.listname.deleteOne({
_id: ctx.params.id
})
ctx.body = res;
},
async page (ctx) {
console.log('page...', ctx.params.page);
ctx.body = await ctx.listname.find({})
},
}
So far, you can see the corresponding effect by using postman to access the interface
http://localhost:3000/api/user