A common service side based nodejs that integrates CURD(for MySql),CORS,static resource.U can build RESTFul Api quickly without repeat coding work
- download dependencies with
npm install
- new a controller in ‘/src/controller’ like userController.define your level-2 url(url name is customized,but please name them after according userController for consistent style and better Maintainability).The methods in baseController accept two params: mysql dataBase table name and http context
const router = require('koa-router')()
const baseController = require('../common/baseController')
/**
* 列表查询
* @type {Router}
*/
module.exports = router.get('/getAll', (ctx)=>{ return baseController.getAll('user',ctx) })
/**
* 主键查询
* @type {Router}
*/
module.exports = router.get('/getUser', (ctx) => { return baseController.getById('user', ctx) })
/**
* 新建用户
* @type {Router}
*/
module.exports = router.post('/newUser', (ctx) => { return baseController.addRecord('user', ctx) })
/**
* 修改用户
* @type {Router}
*/
module.exports = router.put('/editUser', (ctx) => { return baseController.updateRecord('user', ctx) })
/**
* 删除用户
* @type {this}
*/
module.exports = router.delete('/deleteUser', (ctx) => { return baseController.deleteById('user', ctx) })
/**
* 根据id列表批量删除用户
* @type {Router}
*/
module.exports = router.post('/mulDeleteUser', (ctx) => { return baseController.deleteByArr('user', ctx) })
/**
* 条件查询
* @type {Router}
*/
module.exports = router.post('/getUserByFileds', (ctx) => { return baseController.getByFields('user', ctx) })
/**
* 模糊查询
* @type {Router}
*/
module.exports = router.post('/getUserByLikeFileds', (ctx) => { return baseController.getByLike('user', ctx) })
- Import the controller in 'router/index.js' and name the level-1 url
const user = require('../src/controller/userController')
router.use('/user', user.routes(), user.allowedMethods())
-
modify your database config in /config.js
-
run it with
node index.js
- Base add:
http.post('user/newUser', {
name: 'wujianx',
location: 'wuhan'
})
- Base select:
http.get('user/getAll?page=1&pageSize=10')
- Base Select by field:
http.post('user/getUserByFileds', {
name: 'wujianx'
})
- Base modify:
http.put('user/editUser', {
name: 'jiangxiny'
})
- Base delete:
http.delete('user/deleteUser?id=1')
- Base Multiple delete:
http.delete('user/mulDeleteUser', {
arr: [1, 2, 3]
})