# mini-koa

> mini-koa是一个精简版koa和koa-router的结合，是一个值得学习Node网络框架封装的项目。

Latest version **1.0.0** (published 2019-04-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install mini-koa
pnpm add mini-koa
yarn add mini-koa
bun add mini-koa
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2019-04-18 |
| First published | 2019-04-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 14 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Mask |
| Maintainers | qzcmask |
| Keywords | Node.js, koa, koa-router |

## Links

- npm: https://www.npmjs.com/package/mini-koa
- Repository: https://github.com/qzcmask/mini-koa
- Homepage: https://github.com/qzcmask/mini-koa#readme
- Issues: https://github.com/qzcmask/mini-koa/issues
- npm.io page: https://npm.io/package/mini-koa

## Alternatives

- [express-promise-router](https://npm.io/package/express-promise-router.md) — 736.1K weekly downloads
- [next-usequerystate](https://npm.io/package/next-usequerystate.md) — 29.8K weekly downloads
- [@bitkyc08/opencodex](https://npm.io/package/@bitkyc08/opencodex.md) — 4.6K weekly downloads
- [lynkr](https://npm.io/package/lynkr.md) — 575 weekly downloads
- [baremetal.js](https://npm.io/package/baremetal.js.md) — 42 weekly downloads

## Recent versions

- 1.0.0 (latest) — 2019-04-18

## README

# mini-koa

[`mini-koa`](https://github.com/qzcmask/mini-koa)是一个精简版`koa`和`koa-router`的结合，是一个值得学习`Node`网络框架封装的项目。

这个项目是我根据日常使用`koa`和`koa-router`的经验写的，没有参考过它们两者的源码，项目仅供学习。

`mini-koa`代码很精简，核心代码不超过`300`行，[源代码]((https://github.com/qzcmask/mini-koa))配有详细的注释。

## 安装

### NPM

```
npm i mini-koa -S
```

### 下载文件

可以直接下载项目到本地。

```
cd 项目目录

// 测试服务
npm run example
```

## 用法

用法跟`koa`类似，目前mini-koa支持以下方法：

```
Koa.prototype.use()
Koa.prototype.all()
Koa.prototype.get()
Koa.prototype.post()
Koa.prototype.listen()
Koa.prototype.addRoutes() # 添加子路由

// put/delete等其他方法类似get/post，故不加入源代码中
```

`mini-koa-router`支持以下方法：

```
Koa.prototype.use()
Koa.prototype.all()
Koa.prototype.get()
Koa.prototype.post()
```

其中，`use/all/get/post`等方法的url参数支持`params`写法，即

```
app.get('/user/:id')
```

在路由监听函数内，可以用的变量：

```
ctx.query: 查询参数('/user/abc?a=1')
ctx.params: 路由上的占位变量('/user/:id')
```

## 具体示例

```
// 代码放置在examples/server.js

const { Koa, KoaRouter } = require('mini-koa')
const app = new Koa()
// 路由用法
const userRouter = new KoaRouter({
  prefix: '/user'
})

// 中间件函数
app.use(async (ctx, next) => {
  console.log(`请求url, 请求method: `, ctx.req.url, ctx.req.method)
  await next()
})

// 方法示例
app.get('/get', async ctx => {
  ctx.body = 'hello ,app get'
})

app.post('/post', async ctx => {
  ctx.body = 'hello ,app post'
})

app.all('/all', async ctx => {
  ctx.body = 'hello ,/all 支持所有方法'
})

// 子路由使用示例
userRouter.post('/login', async ctx => {
  ctx.body = 'user login success'
})

userRouter.get('/logout', async ctx => {
  ctx.body = 'user logout success'
})

userRouter.get('/:id', async ctx => {
  ctx.body = '用户id： ' + ctx.params.id
})

// 添加路由
app.addRoutes(userRouter)

// 监听端口
app.listen(3000, () => {
  console.log('> App is listening at port 3000...')
})

```

## 联系作者

作者: Mask

项目地址: [`mini-koa`](https://github.com/qzcmask/mini-koa)

---
_Source: https://npm.io/package/mini-koa · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
