Comments (8)
I'm not sure what you mean exactly, could you provide and example? Are you talking about named routes, or wildcard routes?
You can specify a RegExp
for the path to match anything:
// GET /webhooks/something
app.get(/^\/webhooks\/([^\/]+)\/?$/i, function *() {
console.log(this.params[0]);
// => "something"
});
or
// GET /webhooks/something
app.get("/webhooks/:name", function *() {
console.log(this.params.name);
// => "something"
});
from koa-router.
Yes, I'm talking about named routes.
I mean something like this:
app.get('post', '/post/:id', function *() {...});
// Generates url for post with id = 1
var url = app.pathForRoute('post', { id: 1 });
// ==> '/post/1'
Almost every serious web framework has such feature. It's very useful and helps you follow DRY principle. I was very surprised that even Express doesn't have this feature...
from koa-router.
@th0r do you have some urls to share that point to documentation of such a feature on the frameworks you think about ?
I would call that feature "reverse routing". I suspect you called this "named routes" because for this to work you have to give a name to each route so that you can reference it later.
It could indeed be a good addition for projects using koa-router for the front-end. In simple routing schemes, you can indeed avoid the harcoding of the reverse routes in the templates and hardcode only the name of the route which is better if you want to change the routing later (let's say for versioning purpose for instance).
In your proposal, it seems you are missing the method. It could be something like
API change to be able to define a name for a given route
app.get(routeName, path, mw1, mw2, ..)
app.post(routeName, path, mw1, mw2, ..)
API new : calculate the reverse of a route
app.reverse(routeName, method, params)
I guess that this would simply replace the named params by the params. should it throw when there are missing params or return a partially replace route ?
It should probably re-check that the calculated reverse route still matches the original route as a safeguard mechanism to protect against bugs ('undefined', wrong values)
thoughts?
from koa-router.
@th0r do you have some urls to share that point to documentation of such a feature on the frameworks you think about ?
Python web frameworks: Django, Flask, Pyramid.
Node web frameworks: CompoundJS, Locomotive.
should it throw when there are missing params or return a partially replace route ?
Yes, I think it should.
It should probably re-check that the calculated reverse route still matches the original route as a safeguard mechanism to protect against bugs ('undefined', wrong values)
I think it's unnecessary. It's up to developer to pass there proper params.
from koa-router.
Great! Thanks!
from koa-router.
@th0r I have created an issue for this in the resource module I've separated out in 3.0.0. See alexmingoia/koa-resource-router#2. I haven't had a chance to implement it there.
from koa-router.
@alexmingoia What do you think about optional (/:param1?
) and global (/*
) parameters support?
from koa-router.
@alexmingoia you might want to look at http://stackoverflow.com/questions/10027574/express-js-reverse-url-route-django-style for another implementation.
from koa-router.
Related Issues (20)
- Got 404 rather than 500 HOT 2
- Exceptions throw 404 rather than 500 HOT 1
- Map of middleware doesn't work HOT 3
- Critical Dependency error from outdated koa-compose HOT 1
- Can you please give me npm and GitHub access? HOT 8
- Lack of group functionality like that in laravel
- The ctx. _matchedRoute is not the route I requested! HOT 6
- router.param handler called once per middleware i/o once per request
- Getting 404 for all router.post response HOT 4
- One problem with Router.url()
- Multi routes will be called if the last middleware call next()
- log messages flooding console
- Router GET with parameter and Nested Routes It's possible? HOT 1
- Add readme note about @koa/router HOT 3
- Enable a configuration parameter to restrict URL length
- koa-router v9 not supporting wildcard anymore? HOT 2
- Error: Cannot find module 'koa-router' HOT 2
- router reuse error
- Can not install via npm?
- Update `http-errors` to v2+
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from koa-router.