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 3.2.3 to 3.3.0

37

lib/route.js

@@ -63,2 +63,4 @@ /**

this.middleware.array = middleware;
debug('defined route %s %s', this.methods, this.path);

@@ -159,2 +161,37 @@ };

/**
* Run validations on route named parameters.
*
* @example
*
* router
* .param('user', function *(id, next) {
* this.user = users[id];
* if (!user) return this.status = 404;
* yield next;
* })
* .get('/users/:user', function *(next) {
* this.body = this.user;
* });
*
* @param {String} param
* @param {Function *(id, next)} fn
* @api public
*/
route.param = function(param, fn) {
var middleware = this.middleware.array;
if (this.params.some(function(routeParam) {
return routeParam.name = param;
})) {
middleware.unshift(function *(next) {
yield fn.call(this, this.params[param], next);
});
this.middleware = compose(middleware);
this.middleware.array = middleware;
}
return this;
};
/**
* Safe decodeURIComponent, won't throw any error.

@@ -161,0 +198,0 @@ * If `decodeURIComponent` error happen, just return the original value.

31

lib/router.js

@@ -33,2 +33,3 @@ /**

this.routes = [];
this.params = {};

@@ -150,3 +151,4 @@ // extend application

return this.register.apply(this, args);
this.register.apply(this, args);
return this;
};

@@ -206,2 +208,7 @@

// add parameter middleware
Object.keys(this.params).forEach(function(param) {
route.param(param, this.params[param]);
}, this);
// register route with router

@@ -284,2 +291,10 @@ this.routes.push(route);

router.param = function(param, fn) {
this.params[param] = fn;
this.routes.forEach(function(route) {
route.param(param, fn);
});
return this;
};
/**

@@ -294,6 +309,14 @@ * Extend given `app` with router methods.

router.extendApp = function(app) {
['all', 'redirect', 'register', 'url', 'del'].concat(methods)
var router = this;
app.url = router.url.bind(router);
['all', 'redirect', 'register', 'del', 'param']
.concat(methods)
.forEach(function(method) {
app[method] = Router.prototype[method].bind(this);
}, this);
app[method] = function() {
router[method].apply(router, arguments);
return this;
};
});

@@ -300,0 +323,0 @@ return app;

10

package.json

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

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

@@ -19,3 +19,3 @@ "koa",

"methods": "^1.0.1",
"path-to-regexp": "^0.2.1",
"path-to-regexp": "^1.0.0",
"debug": "^1.0.2",

@@ -25,6 +25,6 @@ "koa-compose": "^2.3.0"

"devDependencies": {
"koa": "^0.3.0",
"should": "^3.1.2",
"koa": "^0.10.0",
"should": "^4.0.4",
"mocha": "^1.17.1",
"supertest": "^0.9.0"
"supertest": "^0.13.0"
},

@@ -31,0 +31,0 @@ "scripts": {

@@ -78,3 +78,3 @@ # Router middleware for [koa](https://github.com/koajs/koa)

```js
```javascript
var api = new Router();

@@ -102,17 +102,15 @@

```javascript
app.get('/', function *(next) {
this.body = 'Hello World!';
});
app.post('/users', function *(next) {
// ...
});
app.put('/users/:id', function *(next) {
// ...
});
app.del('/users/:id', function *(next) {
// ...
});
app
.get('/', function *(next) {
this.body = 'Hello World!';
})
.post('/users', function *(next) {
// ...
})
.put('/users/:id', function *(next) {
// ...
})
.del('/users/:id', function *(next) {
// ...
});
```

@@ -173,12 +171,20 @@

##### Regular expression captures
##### Parameter middleware
Run middleware for named route parameters. Useful for auto-loading or
validation.
```javascript
app.get(/^\/([^\/]+)\/([^\/]+)\/?$/, function *(next) {
console.log(this.params);
// => [ 'programming', 'how-to-node' ]
});
app
.param('user', function *(id, next) {
this.user = users[id];
if (!this.user) return this.status = 404;
yield next;
})
.get('/users/:user', function *(next) {
this.body = this.user;
})
```
#### Regular expressions
##### Regular expressions

@@ -185,0 +191,0 @@ Control route matching exactly by specifying a regular expression instead of

@@ -8,3 +8,3 @@ /**

, request = require('supertest')
, router = require('../../lib/router')
, Router = require('../../lib/router')
, should = require('should')

@@ -16,3 +16,3 @@ , Route = require('../../lib/route');

var app = koa();
app.use(router(app));
app.use(Router(app));
app.get(/^\/blog\/\d{4}-\d{2}-\d{2}\/?$/i, function *(next) {

@@ -32,3 +32,3 @@ this.status = 204;

var app = koa();
app.use(router(app));
app.use(Router(app));
app.get('test', /^\/test\/?/i, function *(next) {

@@ -49,3 +49,3 @@ this.status = 204;

var app = koa();
app.use(router(app));
app.use(Router(app));
app.get(

@@ -74,3 +74,3 @@ '/:category/:title',

var app = koa();
app.use(router(app));
app.use(Router(app));
app.get('/:category/:title', function *(next) {

@@ -94,3 +94,3 @@ this.should.have.property('params');

var app = koa();
app.use(router(app));
app.use(Router(app));
app.get('/:category/:title', function *(next) {

@@ -111,3 +111,3 @@ this.should.have.property('params');

var app = koa();
app.use(router(app));
app.use(Router(app));
app.get(/^\/api\/([^\/]+)\/?/i, function *(next) {

@@ -135,3 +135,3 @@ this.should.have.property('params');

var app = koa();
app.use(router(app));
app.use(Router(app));
app.get(/^\/api\/([^\/]+)\/?/i, function *(next) {

@@ -159,3 +159,3 @@ this.should.have.property('params');

var app = koa();
app.use(router(app));
app.use(Router(app));
app.get(/^\/api(\/.+)?/i, function *(next) {

@@ -183,3 +183,3 @@ this.should.have.property('params');

var app = koa();
app.use(router(app));
app.use(Router(app));
var notexistHandle = undefined;

@@ -200,2 +200,28 @@ (function () {

describe('Route#param()', function() {
it('composes middleware for param fn', function(done) {
var app = koa();
var router = new Router();
var route = new Route('/users/:user', ['GET'], [function *(next) {
this.body = this.user;
}]);
route.param('user', function *(id, next) {
this.user = { name: 'alex' };
if (!id) return this.status = 404;
yield next;
});
router.routes.push(route);
app.use(router.middleware());
request(http.createServer(app.callback()))
.get('/users/3')
.expect(200)
.end(function(err, res) {
if (err) return done(err);
res.should.have.property('body');
res.body.should.have.property('name', 'alex');
done();
});
});
});
describe('Route#url()', function() {

@@ -202,0 +228,0 @@ it('generates route URL', function() {

@@ -12,2 +12,3 @@ /**

, Router = require('../../lib/router')
, Route = require('../../lib/route')
, should = require('should');

@@ -227,8 +228,10 @@

var router = new Router(app);
app.use(router.middleware());
var route = app.all('/', function *(next) {
app.all('/', function *(next) {
this.status = 204;
});
app.use(router.middleware());
router.should.have.property('routes');
router.routes.should.include(route);
router.routes.should.have.property('length', 1);
router.routes[0].should.be.instanceOf(Route);
router.routes[0].should.have.property('path', '/');
done();

@@ -242,7 +245,9 @@ });

var router = new Router(app);
router.should.have.property('register');
router.register.should.be.type('function');
var route = router.register('/', ['GET', 'POST'], function *() {});
app.use(router.middleware());
app.should.have.property('register');
app.register.should.be.type('function');
var route = app.register('/', ['GET', 'POST'], function *() {});
router.routes.should.include(route);
router.routes.should.be.an.instanceOf(Array);
router.routes.should.have.property('length', 1);
router.routes[0].should.have.property('path', '/');
done();

@@ -256,7 +261,9 @@ });

var router = new Router(app);
router.should.have.property('redirect');
router.redirect.should.be.type('function');
router.redirect('/source', '/destination', 302);
app.use(router.middleware());
app.should.have.property('redirect');
app.redirect.should.be.type('function');
var route = app.redirect('/source', '/destination', 302);
router.routes.should.include(route);
router.routes.should.have.property('length', 1);
router.routes[0].should.be.instanceOf(Route);
router.routes[0].should.have.property('path', '/source');
done();

@@ -271,3 +278,3 @@ });

app.get('sign-up-form', '/sign-up-form', function *() {});
var route = app.redirect('home', 'sign-up-form');
app.redirect('home', 'sign-up-form');
request(http.createServer(app.callback()))

@@ -298,2 +305,28 @@ .post('/')

});
describe('Router#param()', function() {
it('runs parameter middleware', function(done) {
var app = koa();
request(http.createServer(
app
.use(Router(app))
.param('user', function *(id, next) {
this.user = { name: 'alex' };
if (!id) return this.status = 404;
yield next;
})
.get('/users/:user', function *(next) {
this.body = this.user;
})
.callback()))
.get('/users/3')
.expect(200)
.end(function(err, res) {
if (err) return done(err);
res.should.have.property('body');
res.body.should.have.property('name', 'alex');
done();
});
});
});
});
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