Skip to content
This repository has been archived by the owner on Aug 17, 2023. It is now read-only.

Commit

Permalink
fixed multi router, #4
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Apr 26, 2017
1 parent c08d421 commit 431f52c
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 7 deletions.
20 changes: 20 additions & 0 deletions examples/multi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict'

const Toa = require('toa')
const Router = require('../')
const app = new Toa()

const router1 = new Router('/abc')
router1.get('/:anything', function () {
this.body = this.params['anything']
})

const router2 = new Router('/abcd')
router2.get('/:anything', function () {
this.body = this.params['anything']
})

app.use(router1.toThunk())
app.use(router2.toThunk())

app.listen(3000)
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Router {
options = options || {}
if (typeof options === 'string') options = {root: options}
this.root = typeof options.root === 'string' ? options.root : '/'
if (!this.root.endsWith('/')) this.root += '/'
this.trie = new Trie(options)
this.middleware = []
this._otherwise = null
Expand Down Expand Up @@ -54,8 +55,7 @@ class Router {
this[ROUTED] = true

if (router.root.length > 1) {
path = path.slice(router.root.length)
if (!path) path = '/'
path = path.slice(router.root.length - 1)
}

let matched = router.trie.match(path)
Expand All @@ -64,7 +64,7 @@ class Router {
let url = matched.tsr
if (matched.fpr) url = matched.fpr
if (router.root.length > 1) {
url = router.root + url
url = router.root + url.slice(1)
}
this.path = url

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"authors": [
"Yan Qing <[email protected]>"
],
"version": "2.1.0",
"version": "2.1.1",
"main": "index.js",
"repository": {
"type": "git",
Expand All @@ -28,10 +28,10 @@
},
"devDependencies": {
"istanbul": "^0.4.5",
"standard": "^9.0.2",
"standard": "^10.0.2",
"supertest": "^3.0.0",
"tman": "^1.6.6",
"toa": "^2.6.2"
"toa": "^2.6.4"
},
"scripts": {
"test": "standard && tman",
Expand Down
44 changes: 43 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ tman.suite('toa-router', function () {

yield request(server)
.get('/api')
.expect(501)
.expect(421)
assert.strictEqual(called, 0)

yield request(server)
Expand Down Expand Up @@ -632,4 +632,46 @@ tman.suite('toa-router', function () {
.expect('some error')
assert.strictEqual(count, 0)
})

tman.it('multi-router', function * () {
let Toa = require('toa')
let app = new Toa()

let router1 = new Router('/abc')
router1.get('/:anything', function () {
this.body = this.params['anything']
})

let router2 = new Router('/abcd')
router2.get('/:anything', function () {
this.body = this.params['anything']
})

app.use(router1.toThunk())
app.use(router2.toThunk())

app.onerror = () => {}
let server = app.listen()

yield request(server).get('/')
.expect(421)
yield request(server).get('/abc')
.expect(421)
yield request(server).get('/abcd')
.expect(421)

yield request(server).get('/abc/')
.expect(200)
.expect('')
yield request(server).get('/abc/123')
.expect(200)
.expect('123')

yield request(server).get('/abcd/')
.expect(200)
.expect('')
yield request(server).get('/abcd/123')
.expect(200)
.expect('123')
})
})

0 comments on commit 431f52c

Please sign in to comment.