Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Removed functionality for regex routes.

  • Loading branch information...
commit 8f7a31db03e5b854d5f33872613a8461a617411e 1 parent ed34b40
@twentyrogersc authored
Showing with 24 additions and 43 deletions.
  1. +10 −17 index.js
  2. +14 −26 test/teller.js
View
27 index.js
@@ -12,17 +12,6 @@ var url = require('url')
var app = {}
var cache = {}
-var routes = {
- get: crossroads.create(),
- post: crossroads.create()
-}
-
-var bypassed = function(req, res) {
- res.show404()
-}
-
-routes.get.bypassed.add(bypassed)
-routes.post.bypassed.add(bypassed)
app.settings = function(opts) {
@@ -36,9 +25,9 @@ var add = {}
add.static = function(opts) {
var lastChar = opts.route.charAt(opts.route.length-1)
var route = lastChar !== '/' ? opts.route+'/' : opts.route
- var routeExp = route+':file*:'
+ var routeExp = 'GET '+route+':file*:'
- routes.get.addRoute(routeExp, function(req, res) {
+ crossroads.addRoute(routeExp, function(req, res) {
var file = req.url.pathname.replace(route, '/')
file = path.join(opts.dir, file)
if (path.existsSync(file)) filed(file).pipe(res)
@@ -73,6 +62,10 @@ var show404 = function() {
this.send('<h1>404, not found</h1>', 404)
}
+crossroads.bypassed.add(function(req, res) {
+ res.show404()
+})
+
var json = function(obj, code) {
var body = JSON.stringify(obj)
this.send(body, code, 'application/json')
@@ -100,8 +93,8 @@ var server = function(req, res) {
res.show404 = show404
req.url = url.parse(req.url)
- var method = req.method.toLowerCase()
- routes[method].parse(req.url.pathname, [req, res])
+ var route = req.method+' '+req.url.pathname
+ crossroads.parse(route, [req, res])
}
app.listen = function(port) {
@@ -111,7 +104,7 @@ app.listen = function(port) {
app.get = function(route, cb) {
- routes.get.addRoute(route, function(req, res) {
+ crossroads.addRoute('GET '+route, function(req, res) {
req.query = qs.parse(req.url.query)
cb(req, res)
})
@@ -119,7 +112,7 @@ app.get = function(route, cb) {
}
app.post = function(route, cb) {
- routes.post.addRoute(route, function(req, res) {
+ crossroads.addRoute('POST '+route, function(req, res) {
var form = new formidable.IncomingForm()
form.parse(req, function(err, fields, files) {
req.body = fields
View
40 test/teller.js
@@ -5,9 +5,6 @@ require('../index')
.get('/', function(req, res) {
res.end('get')
})
- .get(/^\/(reg|ex)$/, function(req, res) {
- res.end('regex')
- })
.get('/query', function(req, res) {
res.end(req.query.a)
})
@@ -24,12 +21,12 @@ require('../index')
.get('/404', function(req, res) {
res.show404()
})
+ .get('/send', function(req, res) {
+ res.send('a', 201, 'a/b')
+ })
.post('/post', function(req, res) {
res.end('post')
})
- .post(/^\/[a-z][0-9]$/, function(req, res) {
- res.end('regex')
- })
.post('/body', function(req, res) {
res.end(req.body.a)
})
@@ -51,14 +48,6 @@ describe('app', function() {
done()
})
})
- it('should match a regex route', function(done) {
- request('http://localhost:1234/ex', function(err, res, body) {
- should.not.exist(err)
- should.exist(res)
- body.should.equal('regex')
- done()
- })
- })
it('should pass through parsed query string', function(done) {
request('http://localhost:1234/query?a=b', function(err, res, body) {
should.not.exist(err)
@@ -90,18 +79,6 @@ describe('app', function() {
done()
})
})
- it('should match a regex route', function(done) {
- request({
- form: {},
- url: 'http://localhost:1234/a1',
- method: 'post'
- }, function(err, res, body) {
- should.not.exist(err)
- should.exist(res)
- body.should.equal('regex')
- done()
- })
- })
it('should pass through parsed form fields', function(done) {
request({
form: { a: 1 },
@@ -205,6 +182,17 @@ describe('res', function() {
})
})
+ describe('send()', function() {
+ it('should respond with correct headers and status code', function(done) {
+ request('http://localhost:1234/send', function(err, res, body) {
+ res.headers['content-type'].should.equal('a/b')
+ res.statusCode.should.equal(201)
+ body.should.equal('a')
+ done()
+ })
+ })
+ })
+
describe('show404()', function() {
it('should show the default 404 page', function(done) {
request('http://localhost:1234/404', function(err, res, body) {
Please sign in to comment.
Something went wrong with that request. Please try again.