Socket
Socket
Sign inDemoInstall

koa-router

Package Overview
Dependencies
Maintainers
1
Versions
91
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-router - npm Package Compare versions

Comparing version 2.3.1 to 2.4.0

4

lib/resource.js

@@ -117,6 +117,6 @@ /**

route.path = base + ':' + this.id + route.path;
route.paramNames = [];
route.regexp = pathToRegexp(route.path, route.paramNames);
route.params = [];
route.regexp = pathToRegexp(route.path, route.params);
}
return this;
};

@@ -57,3 +57,3 @@ /**

return function *dispatch(next) {
var route;
var routes = [];

@@ -64,9 +64,11 @@ this.params = [];

debug('%s %s', this.method, this.path);
if (!(route = router.match(this.method, this.path, this.params))) {
if (!(routes = router.match(this.method, this.path, this.params))) {
return yield next;
}
// Dispatch route middleware
debug('dispatch "%s" %s', route.path, route.regexp);
yield route.middleware.call(this, next);
// Dispatch route middlewares
for (var i=0; i<routes.length; i++) {
debug('dispatch "%s" %s', routes[i].path, routes[i].regexp);
yield routes[i].middleware.call(this, next);
}
};

@@ -198,2 +200,3 @@ };

var routes = this.routes[method.toLowerCase()];
var matchingRoutes = [];

@@ -204,7 +207,12 @@ for (var len = routes.length, i=0; i<len; i++) {

if (routes[i].match(method, path, params)) {
return routes[i];
debug('match "%s" %s', routes[i].path, routes[i].regexp);
matchingRoutes.push(routes[i]);
}
}
return false;
if (matchingRoutes.length > 0) {
return matchingRoutes;
} else {
return false;
}
};

@@ -211,0 +219,0 @@

@@ -9,3 +9,3 @@ {

"author": "Alex Mingoia <talk@alexmingoia.com>",
"version": "2.3.1",
"version": "2.4.0",
"keywords": [

@@ -25,3 +25,3 @@ "koa",

"devDependencies": {
"koa": "0.0.2",
"koa": "0.3.0",
"mocha": "1.12.0",

@@ -32,3 +32,3 @@ "should": "1.2.2",

"scripts": {
"test": "node_modules/mocha/bin/mocha --harmony-generators --recursive"
"test": "NODE_ENV=test node_modules/mocha/bin/mocha --harmony-generators --recursive"
},

@@ -35,0 +35,0 @@ "engines": {

@@ -63,11 +63,24 @@ /**

app.use(router.middleware());
var forums = app.resource('forums', { index: function *() {} });
var threads = app.resource('threads', { index: function *() {} });
var forums = app.resource('forums', {
index: function *() {}
});
var threads = app.resource('threads', {
index: function *() {},
show: function *() {
should.exist(this.params);
this.params.should.have.property('forum', '54');
this.params.should.have.property('thread', '12');
this.status = 200;
}
});
forums.add(threads);
threads.base.should.equal('/forums/:forum/threads');
should.exist(router.routes.get[1]);
router.routes.get[1].should.be.a('object');
router.routes.get[1].should.have.property('path', '/forums/:forum/threads');
done();
request(http.createServer(app.callback()))
.get('/forums/54/threads/12')
.expect(200)
.end(function(err, res) {
if (err) return done(err);
done();
});
});
});

@@ -63,10 +63,57 @@ /**

it('supports generators for route middleware', function(done) {
it('matches multiple routes', function(done) {
var app = koa();
var counter = 0;
app.use(Router(app));
app.use(function(next) {
return function *() {
app.get('/:lastname', function *(next) {
this.should.have.property('params');
this.params.should.have.property('lastname', 'smith');
this.status = 204;
counter++;
});
app.get('/:surname', function *(next) {
this.should.have.property('params');
this.params.should.have.property('surname', 'smith');
this.status = 204;
counter++;
});
var server = http.createServer(app.callback());
request(server)
.get('/smith')
.expect(204)
.end(function(err, res) {
if (err) return done(err);
counter.should.equal(2);
done();
};
});
});
it('no matches after throw', function(done) {
var app = koa();
var counter = 0;
app.use(Router(app));
app.get('/', function *(next) {
counter++;
this.throw(403);
});
app.get('/', function *(next) {
counter++;
});
var server = http.createServer(app.callback());
request(server)
.get('/')
.expect(403)
.end(function(err, res) {
if (err) return done(err);
counter.should.equal(1);
done();
});
});
it('supports generators for route middleware', function(done) {
var app = koa();
app.use(Router(app));
app.use(function *() {
done();
});
var readVersion = function() {

@@ -73,0 +120,0 @@ return function(fn) {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc