Skip to content

Commit 8c17fd2

Browse files
committed
ADDED: add a simple koa route imp and it's test case
1 parent a9d0833 commit 8c17fd2

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

units/simple-server-router-test.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
const
2+
var Koa = require('koa')
3+
, http = require('http')
4+
, request = require('supertest')
5+
, should = require('should')
6+
, Route = require('./simple-server-router.js')
7+
8+
async function writeCtxBodyAuthorName (ctx, next) {
9+
ctx.body = {
10+
Author: 'ZWkang'
11+
}
12+
return await next()
13+
}
14+
async function writeCtxBodyMuti (ctx, next) {
15+
ctx.body.muti = true
16+
return await next()
17+
}
18+
describe('Route#middlerware', function() {
19+
it('test single Route case', function(done) {
20+
var app = new Koa();
21+
var router = new Route();
22+
23+
router.register(/\/singleMiddler/, writeCtxBodyAuthorName)
24+
25+
app.use(router.createMiddlerware());
26+
request(http.createServer(app.callback()))
27+
.get('/singleMiddler')
28+
.expect(200)
29+
.end(function(err, res) {
30+
if (err) return done(err);
31+
res.should.have.property('body');
32+
res.body.should.have.property('Author', 'ZWkang');
33+
done();
34+
});
35+
});
36+
it('test muti Routes case', function(done) {
37+
var app = new Koa();
38+
var router = new Route({
39+
muti: true
40+
});
41+
42+
router.register(/\/singleMiddler/, writeCtxBodyAuthorName)
43+
router.register(/\/singleMiddlersTest/, writeCtxBodyMuti)
44+
app.use(router.createMiddlerware());
45+
request(http.createServer(app.callback()))
46+
.get('/singleMiddlersTest')
47+
.expect(200)
48+
.end(function(err, res) {
49+
if (err) return done(err);
50+
res.should.have.property('body');
51+
res.body.should.have.property('Author', 'ZWkang');
52+
res.body.should.have.property('muti', true);
53+
done();
54+
});
55+
});
56+
it('test default should be 404 status code', function(done) {
57+
var app = new Koa();
58+
var router = new Route();
59+
60+
router.register(/\/singleMiddler/, writeCtxBodyAuthorName)
61+
router.register(/\/singleMiddlersTest/, writeCtxBodyMuti)
62+
app.use(router.createMiddlerware());
63+
request(http.createServer(app.callback()))
64+
.get('/iwant404')
65+
.expect(404)
66+
.end(function(err, res) {
67+
if (err) return done(err);
68+
done();
69+
});
70+
});
71+
})

units/simple-server-router.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// const pathToRegexp = require('path-to-regexp');
2+
const debug = require('debug')('kang-route');
3+
// const methods = require('methods');
4+
const compose = require('koa-compose')
5+
6+
const defaultOptions = {
7+
muti: false
8+
}
9+
function Route(options) {
10+
this.middlerwares = []
11+
this.options = options || defaultOptions
12+
}
13+
14+
Route.prototype.register = function (regex, middlerware) {
15+
this.middlerwares.push({
16+
regex,
17+
middlerware
18+
})
19+
}
20+
Route.prototype.createMiddlerware = function () {
21+
const { muti } = this.options
22+
return async (ctx, next) => {
23+
const { req } = ctx
24+
const len = this.middlerwares.length
25+
const url = ctx.url
26+
const middlerwares = []
27+
for(let i = 0; i < len; i++) {
28+
const middlerware = this.middlerwares[i]
29+
const isRegexp = middlerware['regex'].exec(url)
30+
if(isRegexp) {
31+
middlerwares.push(middlerware['middlerware'])
32+
if(i === len-1 || !muti) {
33+
return compose(middlerwares)(ctx,next)
34+
}
35+
}
36+
}
37+
noop(ctx, next)
38+
return await next()
39+
}
40+
}
41+
42+
function noop (ctx, next) {
43+
ctx.status = 404
44+
ctx.res.end()
45+
}
46+
47+
module.exports = Route

0 commit comments

Comments
 (0)