koa2-starter

Koa2 starter RESTful application

Stars
6
Committers
1

Koa2 starter REST API application

Technologies:

  • Koa v2
  • MySQL
  • JSON Schema
  • Yarn
  • ESLint
  • Git hooks

Getting started

Create data base:

CREATE DATABASE `koa2-starter` CHARACTER SET utf8 COLLATE utf8_general_ci;

Install Sequelize CLI:

$ npm i -g sequelize-cli

Install dependencies:

$ yarn install

Make migrations and seeds:

$ sequelize db:migrate
$ sequelize db:seed:all

Run locally:

$ npm start

Project structure

.
└── app/  --> Application files
    ├── config/
    |   ├── app.confg.js    --> App config: port, base url, etc...
    |   └── database.json   --> databes configuration file generated by sequelize cli
    ├── middlewares/  --> All custom middlewares should be stored in this folder
    ├── migrations/   --> Migrations generated by sequelize cli
    ├── models/       --> Models generated sequelize cli
    ├── seeders/      --> Seeds generated sequelize cli
    ├── src/          --> All endpoints should be stored inside this folder
    |   └── user/
    |       ├── router.js           --> Required file. Should be inside each endpoint. Contains koa router instance.
    |       ├── user.controller.js  --> Routes handlers
    |       └── schemas/
    |           ├── index.js            --> Collect all schemas  
    |           └── create.schema.json  --> JSON Schema
    ├── utils/        --> Application common utils
    └── index.js      --> Application entry point

Endpoints

$ http GET localhost:3000/user
{
  "users": [
    {
      "createdAt": "2017-01-22T14:21:46.000Z",
      "email": "[email protected]",
      "first_name": "foo",
      "id": 4,
      "last_name": "bar",
      "status": "active",
      "updatedAt": "2017-01-22T14:21:46.000Z"
    }
  ]
}
$ http GET localhost:3000/user/:id
{
  "user": {
    "createdAt": "2017-01-22T14:21:46.000Z",
    "email": "[email protected]",
    "first_name": "foo",
    "id": 4,
    "last_name": "bar",
    "status": "active",
    "updatedAt": "2017-01-22T14:21:46.000Z"
  }
}
$ http POST localhost:3000/user first_name=foo last_name=bar password=qwerty [email protected]
{
  "user": {
    "createdAt": "2017-01-22T14:21:46.000Z",
    "email": "[email protected]",
    "first_name": "foo",
    "id": 4,
    "last_name": "bar",
    "status": "active",
    "updatedAt": "2017-01-22T14:21:46.000Z"
  }
}
$ http PUT localhost:3000/user first_name=another_name last_name=bar [email protected]
{
  "user": {
    "createdAt": "2017-01-22T14:21:46.000Z",
    "email": "[email protected]",
    "first_name": "another_name",
    "id": 4,
    "last_name": "bar",
    "status": "active",
    "updatedAt": "2017-01-22T14:21:46.000Z"
  }
}
$ http DELETE localhost:3000/user/:id
"204 No Content"

TODO

  • Add unit tests
  • Move validator to separate repo
  • Add production deployment system
  • Add /login && /posts
  • Add auth checking
  • Add ACLs