Git Product home page Git Product logo

koa2-api-boilerplate's Introduction

#koa2-api-boilerplate js-standard-style

Boilerplate for building APIs with koa2 and mongodb.

This project covers basic necessities of most APIs.

  • Authentication (passport & jwt)
  • Database (mongoose)
  • Testing (mocha)
  • Doc generation with apidoc
  • linting using standard

##Requirements

  • node ^4.0.0
  • npm ^3.0.0

##Installation

git clone https://github.com/adrianObel/koa2-api-boilerplate.git

##Features

##Structure

├── bin
│   └── server.js            # Bootstrapping and entry point
├── config                   # Server configuration settings
│   ├── env                  # Environment specific config
│   │   ├── common.js
│   │   ├── development.js
│   │   ├── production.js
│   │   └── test.js
│   ├── index.js             # Config entrypoint - exports config according to envionrment and commons
│   └── passport.js          # Passportjs config of strategies
├── src                      # Source code
│   ├── modules
│   │   ├── controller.js    # Module-specific controllers
│   │   └── router.js        # Router definitions for module
│   ├── models               # Mongoose models
│   └── middleware           # Custom middleware
│       └── validators       # Validation middleware
└── test                     # Unit tests

##Usage

  • npm start Start server on live mode
  • npm run dev Start server on dev mode with nodemon
  • npm run docs Generate API documentation
  • npm test Run mocha tests

##Documentation API documentation is written inline and generated by apidoc.

Visit http://localhost:5000/docs/ to view docs

##License MIT

koa2-api-boilerplate's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

koa2-api-boilerplate's Issues

Example routes for beginners :)

So this boilerplate is absolutely fantastic...
Just wondering if you could stick in the most basic examples of a resource route showing an authenticated route and a non-authenticated route.

/home
/dashboard     [auth required]

it would be super helpful for beginners (like me).
thanks!

ES2016

Would there be a lot of work to use ES2016 with this boilerplate? Or is just matter of replacing babel-preset-es2015-node6 with babel-preset-es2016 ?

Changes for v3

Here are a few things I'm thinking about implementing for V3 of this boilerplate.

  • Replace JSONDoc with API Blueprint + Aglio for documentation
  • Use Dredd for Documentation Validation / testing
  • Docker

Add API documentation

Going through a few options regarding docs.

I'm thinking an inline solution (jsdoc style) instead of having to maintain a separate mark up file would be best.

Deprecated Code

koa deprecated Support for generators will be removed in v3. See the documentation for examples of how to convert old middleware https://github.com/koajs/koa/blob/master/docs/migration.md bin/server.js:59:5
(node:7253) DeprecationWarning: open() is deprecated in mongoose >= 4.11.0, use openUri() instead, or set the useMongoClient option if using connect() or createConnection()

Question

First of all thank you very much for this boilerplate, it's great work and has been tremendously helpful for me to gain an understanding of koa2 and async/await.

I'm struggling to understand the purpose of these pieces of code in /modules/index.js:
const lastHandler = handlers.pop();
return await lastHandler(ctx)

From what I can tell this is so you can check if it is the lasthandler by checking if next exists, but why would you need to do this?

Test runs

Just pulled project and tested it a bit, but what to do with command npm test? As its fails mostly because of ctx input? In a way of: TypeError: Cannot read property 'body' of undefined ?

How to use OAuth strategy?

Hi,

Thanks for the amazing project!
I am using the boilerplate to setup my api and I want to allow login using Fb and Google. The login functionality using local strategy works fine. However, I am unable to integrate it with Fb, Github strategies.

When I access the route http://localhost:3000/auth/github, I get a 404 with the given code:

config/passport.js

passport.use('github', new Strategy({
  clientID: '<client-id>',
  clientSecret: '<secret-token>',
  callbackURL: 'http://localhost:3000/auth/github/callback'
}, async (accessToken, refreshToken, profile, done) => {
    let user = profile
    done(null, user)
}))

controller.js

export async function authUserPre(ctx, next) {
   return passport.authenticate('github')
 }

 export async function authUser(ctx, next) {
   return passport.authenticate({
     successRedirect: '/',
     failureRedirect: '/'
   })
 }

routes.js

export const baseUrl = '/auth'
export default [
  {
    method: 'GET',
    route: '/github',
    handlers: [
      auth.authUserPre
    ]
  },
  {
    method: 'GET',
    route: '/github/callback',
    handlers: [
      auth.authUser
    ]
  }
]

Async/Await in tests?

I've been following this project as a good baseline for testing my Koa2 API, and was wondering what you all think about using async/await with Mocha/Chai/Supertest.

It seems fitting considering the entire API uses async/await as well.

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.