• Stars
    star
    272
  • Rank 151,235 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 9 years ago
  • Updated about 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A boilerplate for writing Koa 2 apps in ES7 with Babel.

Koa Boilerplate

KoaJs Slack

A boilerplate for writing beautiful async-await-based Koa API's using babel for Node v8.0 and above!. 🚀

Setting up shop

Clone this repo and adjust details in package.json. Remove the .git directory and git init to start fresh.

Read on to learn how to actually start being productive.

What's in the package?

  • 🏃‍♀️   npm run scripts, see next section for details.
  • 🛣   awilix-koa for declarative routing.
  • 🗣   babel with env presets, transform-runtime + transform-object-rest-spread plugins and sourcemaps.
  • 🃏   jest for testing with some API testing helpers.
  • 💪   koa-bodyparser for parsing request bodies.
  •   eslint with standard, works with ES7 thanks to babel-eslint.
  • 👀   prettier code formatting, configured to work with eslint out of the box.
  • 🐶   husky + lint-staged to lint and format files when committing.
  •   @koa/cors CORS middleware for cross-domain requests.
  • 🕵️‍♀️   nodemon for development to auto-restart when your files change.
  • 🛠   koa-respond for helper functions on the context.
  • 📄    yenv for environment variable management.
  • 💉    awilix for dependency injection / IoC.
  • ⚠️    fejl for assertions and errors.

npm run scripts

There are a few defined run scripts, here's a list of them with a description of what they do. To run them, simply execute npm run <script name> - e.g. npm run dev

  • start: Used by the production environment to start the app. This will run a compiled version, so you need to execute build first.
  • build: Runs the babel CLI to compile the app. Files are emitted to dist/.
  • dev: Runs the app in development mode - uses babel-node to compile on-the-fly. Also uses nodemon to automatically restart when stuff changes.
  • test: Runs tests.
  • cover: Runs tests and collects coverage.
  • lint: Lints + formats the code.

Tip: to pass additional arguments to the actual CLI's being called, do it like in this example:

For npm:

# Note the `--` before the actual arguments.
npm run test -- --debug

For yarn:

# Yarn does not need the `--` before the actual arguments.
yarn test --debug

docker-compose up scripts

For running dev:

# Note: use --build only when you want to build. Usually when you change packages.json
docker-compose up --build

For running test:

docker-compose -f docker-compose.test.yml up

Directory structure

The repository root contains auxiliary files like package.json, .gitignore, etc.

  • src: the actual source for the app goes here. Duh.
    • __tests__: In the source root folder, contains integration tests.
    • routes: API endpoints go here, and are automatically loaded at startup. Please see the section about API endpoints for details.
    • bin: files that are usually executed by npm run scripts, e.g. starting the server.
    • lib: stuff that helps the app start up, e.g. environment, logger, the container configuration, etc.
    • middleware: custom app middleware.
    • services: application services, this is just to illustrate the dynamic discovery of stuff as described in the Dependency injection section.
      • __tests__: Unit tests for files in the services directory.
    • [your directory]: you can plop anything else here, too.
      • __tests__: Unit tests for files in your directory.

Testing

To recap the previous section, src/__tests__ are for integration tests, and any __tests__ folder under src/<folder> are unit tests.

Test files must end with .test.js.

There is a src/__tests__/api-helper.js that makes writing integration tests way easier. Simply replace the example functions with ones matching your own API. The created server instance is closed whenever all tests are done as to not leak resources. This is why it's important to close network connections in the close event!

Environment variables

The environment variables can be reached by importing lib/env.

import { env } from '../lib/env'

Additionally, all environment variables you'd usually find on process.env will be available on this object.

When attempting to access a key (env.PORT for example), if the key does not exist an error is thrown and the process terminated.

In the repository root, you will find a env.yaml, which is where you can set up environment variables so you won't have to do it from your shell. This also makes it more platform-agnostic.

The top-level nodes in the YAML-file contain a set of environment variables. yenv will load the set that matches whatever NODE_ENV says.

I've set it up so anything in tests will override anything in development when running tests.

Actual environment variables will take precedence over the env.yaml file!

See the yenv docs for more info.

API endpoints

Each file in /routes exports a "controller" that awilix-koa will use for routing. Please see awilix-koa docs for more information.

Dependency injection

This boilerplate uses the Awilix container for managing dependencies - please check out the Awilix documentation for details. The container is configured in lib/container.js.

Middleware

Middleware is located in the middleware folder and is not automatically loaded - they should be installed in lib/server.

Author

License

MIT.

More Repositories

1

awilix

Extremely powerful Inversion of Control (IoC) container for Node.JS
TypeScript
2,999
star
2

typesync

Install missing TypeScript typings for dependencies in your package.json.
TypeScript
1,390
star
3

mobx-task

Makes async function state management in MobX fun.
TypeScript
237
star
4

messageformat.net

ICU MessageFormat implementation for .NET.
C#
148
star
5

awilix-koa

Awilix helpers/middleware for Koa 2
TypeScript
129
star
6

yenv

Environment management for Node using YAML.
JavaScript
104
star
7

koa-respond

Koa middleware that adds useful methods to the context.
JavaScript
102
star
8

awilix-express

Awilix helpers/middleware for Express
TypeScript
101
star
9

libx

Collection + Model infrastructure for MobX applications.
TypeScript
99
star
10

httpclientgoodies.net

Useful utilities for the .NET HttpClient.
C#
62
star
11

snicket

Stream Store based on Postgres for Node.JS
TypeScript
40
star
12

screengun

A simple screen recorder for Windows based on ffmpeg
C#
39
star
13

fejl

Error-making utility for Node apps.
TypeScript
35
star
14

logpipe-server

The Logpipe server.
JavaScript
30
star
15

awilix-router-core

Reusable building blocks for writing Awilix router adapters for HTTP frameworks.
TypeScript
25
star
16

validx

Validation library for MobX.
TypeScript
23
star
17

keyblade

Fail fast when accessing undefined properties on objects.
JavaScript
14
star
18

npm-module-boilerplate

A boilerplate for authoring npm modules, with tests and linting.
JavaScript
10
star
19

skadi

A simple object validator/sanitizer based on `is-my-json-valid`.
JavaScript
9
star
20

deltio

A Google Cloud Pub/Sub emulator alternative, written in Rust.
Rust
8
star
21

dration

Duration utilities for JavaScript using primitive types.
TypeScript
6
star
22

icebug

A wrapper around node-inspector and nodemon.
JavaScript
4
star
23

jQuery-Validator.Knockout

KnockoutJS bindings for my jQuery-Validator.
JavaScript
4
star
24

bristol-sentry

Sentry integration for the Bristol logger
JavaScript
3
star
25

smid

Catches errors and returns them. Useful for unit testing.
TypeScript
3
star
26

baconpi-web

BaconPi Web Frontend + Backend
JavaScript
3
star
27

ts-module-boilerplate

TypeScript module boilerplate for Node packages
TypeScript
3
star
28

posish

A tool that makes counting string lines, columns and indexes awesome. Useful for writing parser tests.
JavaScript
2
star
29

total-rename

Utility to replace strings in files and paths.
Go
2
star
30

purge

Windows utility for deleting a directory tree with long paths.
C#
1
star
31

chilli

Rename files to sort-friendly numbers
F#
1
star
32

jQuery-Validator

A formless validation plugin for all kinds of validation - even your own!
JavaScript
1
star