Skip to content

dukevomv/koa-mongo-starter

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

koa-mongo-starter

A boilerplate koaJS setup, using MongoDB as the backend database, structured in a sensible way, including common libraries for routing, configuration, logging, and database access.

Helper functions are also provided for JWT token authentication and templated view rendering.

Motivation

KoaJS is a very minimalistic, unopinionated node framework, providing a thin layer of abstraction over the base node libraries, and while this provides the developer with maximum flexibility, a part of the code is repeated accross similar projects.

This repository aims to include that part of the code, using tried and tested libraries of the node ecosystem. By no means is this project an attempt to cover all use cases. Instead, it focuses on a specific setup that I have found is convenient when working with a Mongo database.

Installation

Simply run npm install to fetch dependencies

Usage

For development purposes, if you have nodemon installed, you can run the application using npm run startor simply nodemon app.

A couple of convenience functions are provided in the ctx via the corresponding middleware:

  • ctx.jwt([token]) returns a promise that resolves to the decoded JWT token provided in the Authorization header.
  • ctx.render(file[,options]) sets the response body to the rendered output of the provided pug template in src/front/templates.

Note: When using JWT functionality, remember to set the jwt.secret config option to a sensible key. The helper function expects the Authorization header to have the following format: Bearer <token> in order for it to parse the token correctly. If the header is missing or malformed, a 400 exception will be thrown. If the token is invalid, the promise returned will be rejected.

Libraries

The following libraries are used in this setup:

  • config , a versatile configuration library
  • winston , a configurable and modular logging library
  • mongoose , a popular and elegant ODM for working with MongoDB
  • bcrypt , a powerful library for password hashing
  • pug , a high-performance templating engine
  • jsonwebtoken , the Auth0 implementation of JSON Web Tokens
  • app-module-path , a useful library to simplify require() paths

Koa Extensions

The project is based on several extensions of the koa framework, in order to easily bootstrap a new application.

About

A basic koa setup with libraries I commonly use

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 99.2%
  • HTML 0.8%