0

REST API gets quick start with Koa Starter Kit

Koa Starter Kit  is a basic boilerplate for the quick starting with Koa2. It was written in ES6 with a little messily primitive Javascript syntax that helps you get started building NodeJS API easier than ever.

Koa Starter Kit includes some lovely common setup:

  • src structure based-on Javascript modules
  • Watching file changes then reload the server
  • Build production with Babel
  • Routing, multi routers
  • Middlewares
  • Authentication with JSON Web Token
  • Authorization with built-in solution
  • CORS accepts request from domain white list

want more? Folk and submit your regular tasks.

Requirement

Koa Starter Kit requires Node >= 7.6 or higher.

Getting Started

First we’ll clone source code from GitHub Repo

Then we need to install the dependencies

Finally, run the serve

Dev’s shortcuts

There are npm commands for dev’s stuff.

  • Run the server during the development
  • Build and transpile code into Javascript for production
  • Run server as production (run the Js transpiled code)
  • Do the UnitTest

The REST Api

Koa Starter Kit has three api endpoints for demonstration purpose.

You need to setup domains white list before you can use any api endpoints in React, Angular or Vuejs application. You’ll find the white list at: src/config/index.js (someone please help me turn the config file into multi enviroments config!)

/auth

Login to get the jwt token.

  • request config:
    • Method: POST
    • Header:

  • input: JSON as Raw body

  • output:

/user

Get user list

  • request config:
    • Method: GET
    • Header:


  • input: NONE
  • output:

/user/:id

Get specific user info (/user/2 ~ get user info who has id = 2)

  • request config:
    • Method: GET
    • Header:
  • input: NONE
  • output:

Hoàng Tựa

Yêu thích lập trình web và tạo ra những thứ đẹp đẽ.

Leave a Reply

Your email address will not be published. Required fields are marked *