
Introduction
init-koa2
is a WIP effort to automate the process of developing a new project with Koa2.
Usually when starting a new Koa app, you'll go through the regular routine
of:
- adding plenty packages like
koa-bodyparser
, koa-router
,
koa-bunyan-logger
, ...etc - Setting up constants.
- Wiring babel.
- ... and much more tedious steps in order to actually start working.
This is a needed node module
Koa is bare-bones (compared to express for instance), it's extremely fast,
but with the cost of loosing a lot of default packages that are almost
needed in every project.
This is a good thing, it allows you to use the packages you need, for the
functionality you need, but this process takes time, and requires a effort
and great attention to have your Koa app initialized properly.
Convention over configuration
What this package does, is basically this:
require('init-koa2')({
port: 3000,
useStaticFiles: {
dir: 'public',
maxage: 0,
hidden: false,
index: 'index.json'
},
useBodyParser: {
enableTypes: [ 'json' ],
encode: 'utf8',
jsonLimit: '1mb',
strict: 'true'
},
useKoaRouter: true
});
router.get('home', '/', (ctx, next) => {
ctx.body = 'Hello World!';
});
So it'll basically allow you to include packages only when you configure
them, while having default configs of course.
In the above example, if you don't configure koa-router
, then it's not
included in your app, if you do, then it's included. That's it.
No need to define and require Koa itself, or the other basic packages
like router, bodyparse, ... etc
This module is starting with the minimum basic packages to get up
and running with a Koa2 app to build REST APIs, GraphQL server, or a
regular Web App running React.
So you can expect support for packages like router, bodyparser, graphql, graphi, ..etc all optional, all configurable, no need for installing and
requiring each one individually, just start with your new project with
require('init-koa2')({Object}: options)
.
Usage:
This module currently make use of the following submodules:
"koa-router": "^7.0.1"
"koa-static": "^2.0.0"
"koa-bunyan-logger": "^1.3.0"
"koa-bodyparser": "^3.2.0"
Init
All you need to do in order to initialize your Koa app is to add this
require
on top of your entry file.
Example:
const init = require('init-koa2');
init();
Or shorter:
require('init-koa2')();
Then just run your app, no need to add anything else, for example,
don't do app = new Koa();
or app.listen(port)
because all of this
was done for you already.
Configuring sub-modules:
Optionally you can configure the above packages like so
require('init-koa2')({ options });
Available options:
port
:
Application port (Default: 3000
)
logging
:
Enable or disable logs (Default: true
)
useStaticFiles
:
Enable/Disable serving static files
Docs: https://www.npmjs.com/package/koa-static#options
Default:
{
dir: 'public',
maxage: 0,
hidden: false,
index: 'index.json'
}
useBodyParser
:
Enable/disable bodyparser (Default: enableTypes: [ 'json' ], encode: 'utf8', jsonLimit: '1mb', strict: 'true' }
)
useEslint
:
Not yet available, coming in release 1.1.0
.
useBunyanLogger
:
Enable/Disable logging for REST requests/respones
Docs: https://www.npmjs.com/package/koa-bunyan-logger
Default: true
Not configurable, can only be enabled or disabled.
useKoaRouter
:
Enable/Disable routing
Docs: https://github.com/alexmingoia/koa-router/tree/master/
Default: true
Not configurable, can only be enabled or disabled. If you want this
to be configurable, please file an issue.
autoInitiateKoa
:
Automatically initiate koa app
instance, and automatically start
the app and listen to the pre-defined port.
Not configurable, always set to true. If you want this
to be configurable, please file an issue.
Ultimate goal:
This project's ultimate goal is to have the development with Koa
so easy to start with, whenever you start writing a Koa app, you
should just start writing it, not waste a lot of time configuring,
and setting up in order to begin actually developing.
Everything should be ready, and manageable from the first minute.
@TODO (Release 1.0.0)
@TODO (Release 1.1.0)
@TODO (Release 2.0.0)
@TODO (Release x.0.0)