Rest API Generator for Koa2
A mostly unopinionated generator for creating Rest APIs using Koa2 and ES2017+ features in a Node.js server environment as well as providing linting and testing support. It provides the setup for compiling, linting and testing your code but doesn't make any further assumptions on how your project should be structured.
Make sure you read the FAQ for more details and info.
- Koa as the web framework.
- ES2017+ support with Babel.
- Automatic polyfill requires based on environment with babel-preset-env.
- Linting with ESLint.
- Testing with Jest.
# Install using npmnpm install -g koa-api-generator# or if you're using Yarnyarn add global koa-api-generator
Create the app:
koa-api /tmp/foo && cd /tmp/foo
npm install# or if you're using Yarnyarn
If you don't use Yarn you can just replace
npm in the commands that follow.
Then you can begin development:
yarn run dev
This will launch a nodemon process for automatic server restarts when your code changes.
Start the test runner in watch mode with:
You can also generate coverage with:
yarn test --coverage
Begin linting in watch mode with:
yarn run lint
Environmental variables in development
The project uses dotenv for setting environmental variables during development. Simply copy
.env.example, rename it to
.env and add your env vars as you see fit.
It is strongly recommended never to check in your .env file to version control. It should only include environment-specific values such as database passwords or API keys used in development. Your production env variables should be different and be set differently depending on your hosting solution.
dotenv is only for development.
Deployment is specific to hosting platform/provider but generally:
yarn run build
will compile your src into
build (via the
prestart hook) and start the compiled application from the
The last command is generally what most hosting providers use to start your application when deployed, so it should take care of everything.
Where is all the configuration for ESLint, Jest and Babel?
package.json. Feel free to extract them in separate respective config files if you like.
Why are you using
babel-register instead of
babel-node contains a small "trap", it loads Babel's polyfill by default. This means that if you use something that needs to be polyfilled, it'll work just fine in development (because
babel-node polyfills it automatically) but it'll break in production because it needs to be explicitely included in Babel's CLI which handles the final build.
In order to avoid such confusions,
babel-register is a more sensible approach in keeping the development and production runtimes equal. By using babel-preset-env only code that's not supported by the running environment is transpiled and any polyfills required are automatically inserted.
MIT License. See the LICENSE file.