Giter Site home page Giter Site logo

Comments (15)

totherik avatar totherik commented on August 10, 2024

Interesting idea. I'll let others comment, too, but I need a little while to think on this. Just so you know you should also be able to do something like this (if it helps):

/api/projects.js
'use strict';

// api/projects.js
module.exports = function projects(router) {

    router.get('/', function (req, res) {
        res.send('ok');
    };

    router.get('/:taskId', function (req, res) {
        res.send('ok');
    };

    router.get('/:taskId/team', function (req, res) {
        res.send('ok');
    };

    router.get('/:taskId/team/:teamId', function (req, res) {
        res.send('ok');
    };

}

Also, if you need it, this version of enrouten allows you to generate urls. Here's a (contrived) example:

router.get({ name: 'projectById', path: '/:taskId' }, function (req, res) {
    res.send('ok');
};

router.get({ name: 'projectTeams', path: '/:taskId/team' }, function (req, res) {
    var task, team, data;

    // get task and teams from somewhere

    data = {
        project: {
            id: task.id,
            uri: req.app.locals.enrouten.path('projectById', { taskId: task.id }),
            teams: [{
                id: team.id,
                uri: req.app.locals.enrouten.path('projectTeamById', { taskId: task.id, teamId: team.id })
            }]
        }    
    };

    res.json(data);
};

router.get({ name: 'projectTeamById', path: '/:taskId/team/:teamId'}, function (req, res) {
    res.send('ok');
};

from express-enrouten.

alexander-heimbuch avatar alexander-heimbuch commented on August 10, 2024

That's a really nice idea. Currently I'm looking for a dynamic routing prefix. The underscored approach could be a good solution.

from express-enrouten.

erikmueller avatar erikmueller commented on August 10, 2024

👍 Like the underscore idea for dynamic folders.

from express-enrouten.

tkambler avatar tkambler commented on August 10, 2024

Yes, please. 👍

from express-enrouten.

jgable avatar jgable commented on August 10, 2024

For what it's worth, I've since changed my API design to have less deeply nested routes so this isn't really applicable to me any more. But, it looks like others might find it useful.

from express-enrouten.

afloyd avatar afloyd commented on August 10, 2024

I love the idea of enrouten. I have been using my "file-magik" module to auto-require/include all my routing files. But I was looking more after learning about express.Router() a while back, and enrouten almost had everything. But the route params feature was something I wanted/needed too.

Then I realized with just a few changes (which ended up being it's own module) I could use my existing module to do just what I was looking for. It doesn't have some of the bells and whistles of this project, but does have some this one doesn't... I wrote this quick and dirty module yesterday/today

Check it out if interested: https://github.com/afloyd/express-routify

from express-enrouten.

aredridel avatar aredridel commented on August 10, 2024

@tlivings should probably chime in here.

from express-enrouten.

tlivings avatar tlivings commented on August 10, 2024

So, I've done something similar to this over at swaggerize-express.

Since I am working off of swagger documents, path params are of the form {param} in the directory structure.

Example:

handlers
  |--users
  |    |--{id}.js
  |    |--{id}
  |        |--foo.js

This will map as:

/users/{id}
/users/{id}/foo

I considered index.js but decided against it since it didn't play well with subresources and index is a valid path name in a url.

from express-enrouten.

tlivings avatar tlivings commented on August 10, 2024

PS: If there is no subresource, the above example would be simplified to:

handlers
  |--users
  |    |--{id}.js

I'd personally like to remove index.js from express-enrouten as well.

from express-enrouten.

sixlettervariables avatar sixlettervariables commented on August 10, 2024

@tlivings: in your model /users would be handled by ./handlers/users.js rather than ./handlers/users/index.js, correct? Going with that approach my question for the express-enrouten side would be how would you handle the root route?

from express-enrouten.

tlivings avatar tlivings commented on August 10, 2024

Yes, that's true. For RESTful services, there will always be a resource. For an application, there can be a root route, so it might not be possible to do away with index.js.

from express-enrouten.

aredridel avatar aredridel commented on August 10, 2024

/ is a perfectly acceptable path for a resource in a REST service too!

from express-enrouten.

tlivings avatar tlivings commented on August 10, 2024

There may not be anything stopping one from exposing resources at / but I think it goes against convention of how resources are typically represented. We can argue offline though 😸

from express-enrouten.

felipeleusin avatar felipeleusin commented on August 10, 2024

+1. I've been making some tests with this and it works ok, you have to remember to initialize the Router with { mergeParams: true } for this to work.

from express-enrouten.

blowsie avatar blowsie commented on August 10, 2024

late to the party here, but Nuxt has this style of routing

from express-enrouten.

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.