Koa RESTful 腳手架分享

X-RESTful-API-Generator-Koa

A RESTful API generator for Koa.

Start

git clone
  npm i
  npm run dev

Demo

demoModule_001

URL: http://localhost:3000/x-restful-api-generator-koa/demoModule_001/loginmysql

Resonse:git

{
    "code": 200,
    "msg": "登陸成功!",
    "data": {
      "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyTmFtZSI6InRlc3QiLCJwYXNzd29yZCI6IjEyMzQ1NiIsImlhdCI6MTUwODkyMTY2OSwiZXhwIjoxNTA5MDA4MDY5fQ.2occtME3kLUDxntJXOz5e1dkspybGIVqbDPRgaE6lZA"
    }
  }

URL: http://localhost:3000/x-restful-api-generator-koa/demoModule_001/listgithub

Response:sql

{
    "code": 200,
    "msg": "查詢列表成功!",
    "data": {
      "count": 300,
      "list": []
    }
  }

URL: http://localhost:3000/x-restful-api-generator-koa/demoModule_001/verify/listnpm

Response:api

{
    "code": 9999,
    "msg": "token無效!請從新登陸!",
    "data": {}
  }

Articles

URL: http://localhost:3000/x-restful-api-generator-koa/articles/addbash

Request Body:restful

{
    title: 'myArticle_001'
    content: 'yyyyyyyyyyyyyyyyyyyyyyy'
  }

Response:app

{
    "code": 200,
    "msg": "添加文章成功!",
    "data": {
      "id": 3,
      "title": "myArticle_001",
      "content": "yyyyyyyyyyyyyyyyyyyyyyy",
      "updatedAt": "2017-10-27T07:52:21.745Z",
      "createdAt": "2017-10-27T07:52:21.745Z"
    }
  }

URL: http://localhost:3000/x-restful-api-generator-koa/articles/updatekoa

Request Body:

{
    id: '3'
    content: 'xxxxxx'
  }

Response:

{
    "code": 200,
    "msg": "編輯文章成功!",
    "data": {
      "id": 3,
      "title": "myArticle_003",
      "content": "xxxxxx",
      "createdAt": "2017-10-26T23:52:21.000Z",
      "updatedAt": "2017-10-27T00:08:55.000Z"
    }
  }

URL: http://localhost:3000/x-restful-api-generator-koa/articles/remove

Request Body:

{
    id: '2'
  }

Response:

{
    "code": 200,
    "msg": "刪除文章成功!",
    "data": 1
  }

URL: http://localhost:3000/x-restful-api-generator-koa/articles/list?pageSize=1&currentPage=1

Response:

{
    "code": 200,
    "msg": "查詢帳號列表成功!",
    "data": {
      "count": 2,
      "list": [
        {
          "id": 1,
          "title": "myArticle_001",
          "content": "yyyyyyyyyyyyyyyyyyyyyyy",
          "createdAt": "2017-10-26T23:46:10.000Z",
          "updatedAt": "2017-10-26T23:46:10.000Z"
        }
      ]
    }
  }

Develop

1.Edit config.js

2.Export the database to schema by sequelize-auto

sequelize-auto -o "./src/schema" -d x-restful-api-generator-koa -h localhost -u root -p 3306 -e mysql

3.Create Modules

apps
    \_ newModules
        Ctrl.js
        Model.js
        Routers.js

TODO

1.error handler middleware
  2.file upload demo
相關文章
相關標籤/搜索