Giter Club home page Giter Club logo

Comments (8)

alexmingoia avatar alexmingoia commented on July 18, 2024

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.

th0r avatar th0r commented on July 18, 2024

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.

jeromew avatar jeromew commented on July 18, 2024

@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 avatar th0r commented on July 18, 2024

@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.

th0r avatar th0r commented on July 18, 2024

Great! Thanks!

from koa-router.

alexmingoia avatar alexmingoia commented on July 18, 2024

@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.

th0r avatar th0r commented on July 18, 2024

@alexmingoia What do you think about optional (/:param1?) and global (/*) parameters support?

from koa-router.

jeromew avatar jeromew commented on July 18, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.