Skip to content

Commit 4c91b73

Browse files
committed
2 parents 17edebc + 4d5df84 commit 4c91b73

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

units/simple-server-router-test.js

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

units/simple-server-router.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const debug = require('debug')('kang-route');
2+
const compose = require('koa-compose');
3+
4+
/**
5+
* use
6+
* 1. require('simple-server-router.js')
7+
* 2. const routers = new Route({
8+
* muti: true| false // 是否重复匹配路由
9+
* })
10+
*
11+
* 3. routers.register(/test/, async (ctx, next) { ctx.body = {name: 'kang'}})
12+
* 4. app.use(routers.createMiddlerware())
13+
*/
14+
const defaultOptions = {
15+
muti: false
16+
}
17+
function Route(options) {
18+
this.middlerwares = []
19+
this.options = options || defaultOptions
20+
}
21+
22+
Route.prototype.register = function (regex, middlerware) {
23+
this.middlerwares.push({
24+
regex,
25+
middlerware
26+
})
27+
}
28+
Route.prototype.createMiddlerware = function () {
29+
const { muti } = this.options
30+
return async (ctx, next) => {
31+
const { req } = ctx
32+
const len = this.middlerwares.length
33+
const url = ctx.url
34+
const middlerwares = []
35+
for(let i = 0; i < len; i++) {
36+
const middlerware = this.middlerwares[i]
37+
const isRegexp = middlerware['regex'].exec(url)
38+
if(isRegexp) {
39+
middlerwares.push(middlerware['middlerware'])
40+
if(i === len-1 || !muti) {
41+
return compose(middlerwares)(ctx,next)
42+
}
43+
}
44+
}
45+
noop(ctx, next)
46+
return await next()
47+
}
48+
}
49+
50+
function noop (ctx, next) {
51+
ctx.status = 404
52+
ctx.res.end()
53+
}
54+
55+
module.exports = Route

0 commit comments

Comments
 (0)