GithubHelp home page GithubHelp logo

koa-server's Introduction

KOA2-BaseServer

1. What is this?

A common service side based nodejs that integrates CURD(for MySql),CORS,static resource.U can build RESTFul Api quickly without repeat coding work

2. How to run it?

  1. download dependencies with
npm install
  1. 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) })
  1. 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())
  1. modify your database config in /config.js

  2. run it with

node index.js

3. How to use it for the api consumer?

  1. Base add:
http.post('user/newUser', {
   name: 'wujianx',
   location: 'wuhan'
})
  1. Base select:
http.get('user/getAll?page=1&pageSize=10')
  1. Base Select by field:
http.post('user/getUserByFileds', {
	name: 'wujianx'
})
  1. Base modify:
http.put('user/editUser', {
	name: 'jiangxiny'
})
  1. Base delete:
http.delete('user/deleteUser?id=1')
  1. Base Multiple delete:
http.delete('user/mulDeleteUser', {
	arr: [1, 2, 3]
})

koa-server's People

Contributors

942778086 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.