1.

What Is Pattern Matched Routes?

Answer»

You can also use REGEX to restrict URL parameter matching. LET's say you need the id to be 5 digits long number. You can use the following ROUTE definition:

var koa = require('koa');
var router = require('koa-router');
var app = koa();
var _ = router();
_.get('/things/:id([0-9]{5})', sendID);
function *sendID(){
this.body = 'id: ' + this.params.id;
}
app.use(_.routes());
app.listen(3000);

Note that this will only match the requests that have a 5 digit long id. You can use more complex regexes to match/validate your routes. If NONE of your routes match the request, you'll get a Not found message as response.

You can also use regex to restrict URL parameter matching. Let's say you need the id to be 5 digits long number. You can use the following route definition:

var koa = require('koa');
var router = require('koa-router');
var app = koa();
var _ = router();
_.get('/things/:id([0-9]{5})', sendID);
function *sendID(){
this.body = 'id: ' + this.params.id;
}
app.use(_.routes());
app.listen(3000);

Note that this will only match the requests that have a 5 digit long id. You can use more complex regexes to match/validate your routes. If none of your routes match the request, you'll get a Not found message as response.



Discussion

No Comment Found