1.0.2 • Published 2 years ago

@koex/body v1.0.2

Weekly downloads
3
License
MIT
Repository
github
Last release
2 years ago

router

NPM version Coverage Status Dependencies Build Status license issues

Simple Router for Koa

Install

$ npm install @koex/router

Usage

// See more in test
import * as router from '@koex/router';

import * as Koa from 'koa';
const app = new Koa();

app.use(router.get('/', async (ctx) => {
  ctx.body = 'home';
}));

app.use(router.get('/health', async (ctx) => {
  ctx.status = 200;
  ctx.body = 'ok';
}));

app.use(router.get('/product/:pid', async (ctx) => {
  ctx.body = ctx.params.pid;
}));

// support middlewares for router
const md5 = crypto.createHash('md5').update('123').digest('hex');

const responseTime = async (ctx, next) => {
  const start = Date.now();
  await next();
  ctx.set('X-Response-Time', Date.now() - start);
};

const requestId = async (ctx, next) => {
  await next();
  const id = md5(ctx.url + Date.now());
  ctx.set('X-Request-Id', id);
};

const handler = async (ctx) => {
  ctx.body = ctx.params.pid + ': ' + ctx.params.cid;
};

app.use(router.get('/product/:pid/:cid', responseTime, requestId, handler));

// fallback
app.use(async (ctx) => {
  ctx.body = {
    name: 'name',
    value: 'value',
  };
});

app.listen(8000, '0.0.0.0', () => {
  console.log('koa server start at port: 8000');
});

Related

1.0.2

2 years ago

1.0.1

2 years ago

0.13.37

2 years ago

0.13.36

3 years ago

0.13.34

3 years ago

0.13.23

3 years ago

0.13.22

3 years ago

0.13.21

3 years ago

0.13.20

3 years ago

0.13.6

3 years ago

0.13.7

3 years ago

0.13.8

3 years ago

0.13.9

3 years ago

0.13.24

3 years ago

0.13.29

3 years ago

0.13.4

3 years ago

0.13.28

3 years ago

0.13.5

3 years ago

0.13.12

3 years ago

0.13.11

3 years ago

0.13.10

3 years ago

0.13.16

3 years ago

0.13.15

3 years ago

0.13.14

3 years ago

0.13.13

3 years ago

0.13.19

3 years ago

0.13.18

3 years ago

0.13.17

3 years ago

0.13.3

3 years ago

0.13.2

3 years ago

0.13.0

4 years ago

0.12.4

4 years ago

0.12.3

4 years ago

0.12.0

4 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago