Skip to content

☂️ A boilerplate for Koa Server with Typescript, Babel, and Rollup

License

Notifications You must be signed in to change notification settings

pdsuwwz/koa-server-boilerplate

Repository files navigation

koa-server-boilerplate

☂️ A boilerplate for Koa Server with Typescript, Babel, and Rollup

Prerequisites

Please make sure that Node.js (>= 10.18.1) is installed on your operating system.

Feature

🌟 Separation Services logic and Controllers.

🍓 The Friendly practice for Koa project.

🌲 Configured routing.

Eslint configuration.

⚡ Fast build with Rollup.

🔥 HMR.

Project structure

├── src
│   ├── controllers/ ---  Server controllers
│   ├── services/    ---  Server services
│   ├── config.ts    ---  About Environments variable
│   ├── main.ts      ---  Entry file
│   └── routes.ts    ---  Configs for routing controllers

Installation

pnpm install

Running Development

pnpm dev

Running Production

The project has built-in a pm2, when the build is completed, you can running the pnpm start, which will automatically manage the process by pm2.

Run pnpm build to build:

pnpm build

All done! Now run this in your terminal:

pnpm start

Simple API

/api is the prefix for all routes.

  • GET /api

Health check.

curl --location --request GET \
'http://localhost:5000/api'
  • GET /api/get_userinfo

Get User Info.

curl --location --request GET \
'http://localhost:5000/api/get_userinfo?userId=1234567'
  • POST /api/gen_image

Generate a image to response.

curl --location --request POST 'http://localhost:5000/api/gen_image' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'url=http://www.google.com' --output test-image.png

Routing

In order to make the routing information more readable and transparent, the form of configuration is adopted here.

You can create an array and then write the routing meta information into the array, and reuse it in the src/routes.ts

const routes: Array<RouteConfig> = [
  {
    path: '/',
    method: 'get',
    action: HomeController.hello
  },
  // here...
]

License

MIT

Releases

No releases published

Packages

No packages published