Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added parsed route variables in req.route.

  • Loading branch information...
commit 9df3b33b838915360f072526aa959a93b4e850e7 1 parent 9e5be91
Craig Rogers authored
Showing with 60 additions and 11 deletions.
  1. +13 −7 index.js
  2. +47 −4 test/teller.js
20 index.js
View
@@ -25,9 +25,7 @@ var add = {}
add.static = function(opts) {
var lastChar = opts.route.charAt(opts.route.length-1)
var route = lastChar !== '/' ? opts.route+'/' : opts.route
- var routeExp = 'GET '+route+':file*:'
-
- crossroads.addRoute(routeExp, function(req, res) {
+ addRoute('GET', route+':file*:', function(req, res) {
var file = req.url.pathname.replace(route, '/')
file = path.join(opts.dir, file)
if (path.existsSync(file)) filed(file).pipe(res)
@@ -53,7 +51,6 @@ var render = function(template, data, code) {
throw new Error('template does not exist')
return
}
-
var html = ejs.render(cache[template], data)
this.send(html, code)
}
@@ -92,14 +89,23 @@ var server = function(req, res) {
res.send = send
res.show404 = show404
req.url = url.parse(req.url)
-
var route = req.method+' '+req.url.pathname
crossroads.parse(route, [req, res])
}
var addRoute = function(method, route, cb) {
- route = method+' '+route
- crossroads.addRoute(route, cb);
+ var match = method+' '+route
+ var ids = crossroads.addRoute(match, function(req, res) {
+ if (ids.length > 0) {
+ req.route = {}
+ for (var i = 0; i < ids.length; i++) {
+ var part = ids[i].replace('*', '')
+ req.route[part] = arguments[i+2]
+ }
+ }
+ cb(req, res)
+ })._paramsIds
+
}
app.listen = function(port) {
51 test/teller.js
View
@@ -3,10 +3,10 @@ var should = require('should')
require('../index')
.get('/', function(req, res) {
- res.end('get')
+ res.send('get')
})
.get('/query', function(req, res) {
- res.end(req.query.a)
+ res.send(req.query.a)
})
.get('/json', function(req, res) {
res.json(req.query, req.query.code)
@@ -24,11 +24,17 @@ require('../index')
.get('/send', function(req, res) {
res.send('a', 201, 'a/b')
})
+ .get('/match/:a:/:b:', function(req, res) {
+ res.json(req.route)
+ })
.post('/post', function(req, res) {
res.end('post')
})
.post('/body', function(req, res) {
- res.end(req.body.a)
+ res.send(req.body.a)
+ })
+ .post('/match/:a:/:b:', function(req, res) {
+ res.json(req.route)
})
.settings({
template: { dir: __dirname },
@@ -56,14 +62,25 @@ describe('app', function() {
done()
})
})
+ it('pass through variables defined in the route', function(done) {
+ request('http://localhost:1234/match/c/d', function(err, res, body) {
+ should.not.exist(err)
+ should.exist(res)
+ var json = JSON.parse(body)
+ json.a.should.equal('c')
+ json.b.should.equal('d')
+ done()
+ })
+ })
it('should return a 404 for an unmatched route', function(done) {
request('http://localhost:1234/doesnotexist', function(err, res, body) {
+ should.not.exist(err)
res.headers['content-type'].should.equal('text/html')
res.statusCode.should.equal(404)
body.should.equal('<h1>404, not found</h1>')
done()
})
- })
+ })
})
describe('post()', function() {
@@ -91,6 +108,32 @@ describe('app', function() {
done()
})
})
+ it('pass through variables defined in the route', function(done) {
+ request({
+ form: {},
+ url: 'http://localhost:1234/match/c/d',
+ method: 'post'
+ }, function(err, res, body) {
+ should.not.exist(err)
+ should.exist(res)
+ var json = JSON.parse(body)
+ json.a.should.equal('c')
+ json.b.should.equal('d')
+ done()
+ })
+ })
+ it('should return a 404 for an unmatched route', function(done) {
+ request({
+ form: {},
+ url: 'http://localhost:1234/doesnotexist',
+ method: 'post'
+ }, function(err, res, body) {
+ res.headers['content-type'].should.equal('text/html')
+ res.statusCode.should.equal(404)
+ body.should.equal('<h1>404, not found</h1>')
+ done()
+ })
+ })
})
describe('settings()', function() {
Please sign in to comment.
Something went wrong with that request. Please try again.