Skip to content

OXOYO/X-RESTful-API-Generator-Koa

Repository files navigation

X-RESTful-API-Generator-Koa

A RESTful API service generator by Koa.

Travis CI Status codecov.io node GitHub package version dependencies Status codebeat badge license Gitter

English | 简体中文

Start

  # clone
  git clone

  # install dependencies
  npm i

  # import the src/x-restful-api-generator-koa.sql into the database.

  # start serve with hot reload
  npm run dev

Node.js >= 7.6.0 required.

Develop Step

1.Edit config.js

2.Export the database to schema by sequelize-auto

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

3.Create module directories and files

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

Build

  npm run build

production

  pm2 start ecosystem.config.js --name x-restful-api-generator-koa --env production

Demo

To run the demo, you need to import the ./src/x-restful-api-generator-koa.sql into Mysql

demoModule_001

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

Resonse:

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

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

Response:

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

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

Response:

  {
    "code": 9999,
    "msg": "token无效!请重新登录!",
    "data": {}
  }

Articles

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

Request Body:

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

Response:

  {
    "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/update

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"
        }
      ]
    }
  }

File Upload

URL: http://localhost:63342/X-RESTful-API-Generator-Koa/src/apps/Files/upload.html

Response:

  {
    "status": 200,
    "msg": "上传成功!",
    "data": {
      "file": {
        "fieldname": "file",
        "originalname": "app.png",
        "encoding": "7bit",
        "mimetype": "image/png",
        "destination": "E:\\Webstorm_WorkSpace\\X-RESTful-API-Generator-Koa\\assets\\uploads",
        "filename": "1510817484098.png",
        "path": "E:\\Webstorm_WorkSpace\\X-RESTful-API-Generator-Koa\\assets\\uploads\\1510817484098.png",
        "size": 958
      },
      "filename": "1510817484098.png",
      "url": "//localhost:3000/assets/uploads/1510817484098.png"
    }
  }

TODO

  1.error handler middleware

License

MIT