Skip to content

Commit

Permalink
added support for router.use
Browse files Browse the repository at this point in the history
  • Loading branch information
spion committed May 10, 2013
1 parent 90ee043 commit 5c5358f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/router.coffee
Expand Up @@ -48,6 +48,21 @@ class Router
if err?.status then status = err.status
res.writeHead(status)
res.end()

# support usePath
req.usePath = req.originalUrl
.substr(0, req.originalUrl.length
- req.url.length)

# support an extended res.send
#res.send = (code, headers, data) ->
# args = send.arguments(code, headers, data)
# res.writeHead(args.code, args.headers)
# if (null != args.data)
# res.write(args.data)
# @end()


urlObj = url.parse(req.url)
p = normalizePathname(urlObj.pathname)
req.path = p
Expand Down Expand Up @@ -197,4 +212,15 @@ module.exports = (parsers) ->
for method in ['GET', 'POST', 'PUT', 'DELETE', 'PATCH']
r.register('all', method, pattern, handlers...)
return handlers
use: (usepath, handlers...) ->
if (!handlers.length)
handlers = [usepath]
usepath = '/'
if (usepath.slice(-1) != '/')
usepath += '/'
handlers.unshift (req, res, next) ->
req.url = '/' + req.params.__path
next()
@all("#{usepath}<path:__path>", handlers...)
return handlers
}
30 changes: 30 additions & 0 deletions test/router.coffee
Expand Up @@ -585,3 +585,33 @@ describe 'RegExp rule', ->
.get('/REGEXPATH/56')
.expect(404, done)

describe 'res.end(data)', ->
router = createRouter()
app = connect()
app.use(router.route)
router.get '/', (req, res) ->
res.end('data')
it 'should write the data', (done) ->
app.request()
.get('/')
.end (res) ->
res.body.should.eql('data')
done()


describe 'router.use', ->
router = createRouter()
app = connect()
app.use(router.route)

router.use '/test', (req, res) ->
res.end(req.url)

it 'should modify the url', (done) ->
app.request()
.get('/test/url')
.end (res) ->
res.body.should.eql('/url')
done()


0 comments on commit 5c5358f

Please sign in to comment.