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

Commit

Permalink
fixed middleware error
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Aug 25, 2016
1 parent 0e5c90a commit 82f4b83
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 155 deletions.
74 changes: 38 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,51 +18,53 @@ A trie router for toa.

## Demo
```js
'use strict'
// **Github:** https://github.com/toajs/toa
//
// **License:** MIT
var Toa = require('toa')
var Router = require('toa-router')
const Toa = require('toa')
const Router = require('toa-router')

var router = new Router()
const app = Toa()
const APIrouter = new Router('/api')
const otherRouter = new Router()

router.use(function () {
console.log('Hello, middleware.')
app.use(function () {
this.state.ip = this.ip
})

router
.get('', function () {
// sync handle
this.body = this.method + ' ' + this.path
})
.get('/promise', function () {
// promise handle
var ctx = this
return Promise.resolve().then(function () {
ctx.body = ctx.method + ' ' + ctx.path
})
})
.get('/thunk', function () {
// thunk handle
return function (done) {
this.body = this.method + ' ' + this.path
done()
}
})
.get('/generator', function *() {
// generator handle
this.body = this.method + ' ' + this.path
})
APIrouter.use(function * () {
this.state.path = this.path
this.state.token = yield Promise.resolve({uid: 'uidxxx'})
this.state.router = yield Promise.resolve('APIrouter') // some async task
})

var app = Toa(function *() {
yield router
otherRouter.use(function * () {
this.state.path = this.path
this.state.router = yield Promise.resolve('otherRouter') // some async task
})

APIrouter.get('/user', function () {
this.state.user = 'user'
this.body = this.state
})

app.listen(3000, function () {
console.log('Listened 3000')
APIrouter.get('(*)', function () {
this.body = this.state
})

otherRouter.get('(*)', function () {
this.body = this.state
})

app.use(APIrouter.toThunk()) // we should use APIrouter firstly
app.use(otherRouter.toThunk())

app.listen(3000)

// Please try GET:
// http://localhost:3000
// http://localhost:3000/abc
// http://localhost:3000/abc/efg
// http://localhost:3000/api
// http://localhost:3000/api/abc
// http://localhost:3000/api/abc/efg
```

## Installation
Expand Down
132 changes: 34 additions & 98 deletions examples/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,114 +2,50 @@
// **Github:** https://github.com/toajs/toa
//
// **License:** MIT
var Toa = require('toa')
var Router = require('..')
const Toa = require('toa')
const Router = require('..')

var mockPosts = [{
id: '0',
title: 'post 1',
content: 'content 1'
}, {
id: '1',
title: 'post 2',
content: 'content 2'
}, {
id: '2',
title: 'post 3',
content: 'content 3'
}]
const app = Toa()
const APIrouter = new Router('/api')
const otherRouter = new Router()

var mockTasks = [{
id: '0',
title: 'task 1',
content: 'content 1'
}, {
id: '1',
title: 'task 2',
content: 'content 2'
}]

var router = new Router('/api')
var router2 = new Router()
app.use(function () {
this.state.ip = this.ip
})

router.get('/', function () {
this.body = 'Hi, toa router'
APIrouter.use(function * () {
this.state.path = this.path
this.state.token = yield Promise.resolve({uid: 'uidxxx'})
this.state.router = yield Promise.resolve('APIrouter') // some async task
})

router
.define('/:type(posts|tasks)')
.get(function () {
var data = null
switch (this.params.type) {
case 'posts':
data = mockPosts
break
case 'tasks':
data = mockTasks
break
}
if (data) this.body = resJSON(data)
else this.throw(404, this.path + ' is not found!')
})
otherRouter.use(function * () {
this.state.path = this.path
this.state.router = yield Promise.resolve('otherRouter') // some async task
})

router
.define('/:type(posts|tasks)/:id([0-9]+)')
.get(function () {
var data = null
switch (this.params.type) {
case 'posts':
data = mockPosts[this.params.id]
break
case 'tasks':
data = mockTasks[this.params.id]
break
}
if (data) this.body = resJSON(data)
else this.throw(404, this.path + ' is not found!')
})
.post(function () {
var data = null
switch (this.params.type) {
case 'posts':
data = mockPosts[this.params.id]
break
case 'tasks':
data = mockTasks[this.params.id]
break
}
if (data) this.body = resJSON(data)
else this.throw(404, this.path + ' is not found!')
})
.del(function () {
var data = null
switch (this.params.type) {
case 'posts':
data = mockPosts[this.params.id]
break
case 'tasks':
data = mockTasks[this.params.id]
break
}
if (data) this.body = resJSON(data)
else this.throw(404, this.path + ' is not found!')
})
APIrouter.get('/user', function () {
this.state.user = 'user'
this.body = this.state
})

router2.get('/:others(*)', function () {
this.body = 'Path is: ' + this.params.others
APIrouter.get('(*)', function () {
this.body = this.state
})

var app = Toa(function * () {
yield [
router.route(this),
router2.route(this)
]
otherRouter.get('(*)', function () {
this.body = this.state
})

app.use(APIrouter.toThunk()) // we should use APIrouter firstly
app.use(otherRouter.toThunk())

app.listen(3000)

function resJSON (data) {
return {
data: data,
timestamp: Date.now()
}
}
// Please try GET:
// http://localhost:3000
// http://localhost:3000/abc
// http://localhost:3000/abc/efg
// http://localhost:3000/api
// http://localhost:3000/api/abc
// http://localhost:3000/api/abc/efg
9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,19 @@ Router.prototype.route = function (context) {
var preHooks = this._routerState.preHooks.slice()

function worker (ctx, handler) {
return thunk.seq.call(ctx, preHooks)(function () { return handler })
return thunk.seq.call(ctx, preHooks)(function (err) {
if (err != null) throw err
return handler
})
}

return thunk.call(context, function (done) {
var normalPath = path.normalize(this.path).replace(/\\/g, '/')
var method = this.method

if (this.routedPath || (state.root && (normalPath + '/').indexOf(state.root + '/') !== 0)) return done()
if (this.routedPath || (state.root && (normalPath + '/').indexOf(state.root + '/') !== 0)) {
return done()
}
this.routedPath = this.request.routedPath = normalPath
normalPath = normalPath.replace(state.root, '')

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"authors": [
"Yan Qing <[email protected]>"
],
"version": "1.5.0",
"version": "1.5.1",
"main": "index.js",
"repository": {
"type": "git",
Expand Down
Loading

0 comments on commit 82f4b83

Please sign in to comment.