URL Generation for Routes in Express












17















I'm considering using Express framework in my next node.js project. However, a stumbling block for me is non-existence of URL generation for routes like in most other non-Sinatra based frameworks, examples- Django, Flask, Rails etc.



I tried looking for some Connect middleware to serve my task and I did find Barista, Escort, Sherpa and the likes but looking at their GitHub pages, all seem dead and in-active. So, I don't want to go for something that's not maintained anymore for obvious reasons.



My main concern here is that the project may get really large and it WILL be a pain to update URLs in every page whenever business and/or aesthetic requirements change.



Is there something that I failed to see in the docs/tests? If not, then how do I extend the routing framework in Express to do URL generation and make this wrapper available in my views as well as controller functions?



UPDATE: (22/3/2012) I found this page: https://github.com/clyfe/tweet_express/wiki/TODO which specified some routers that do URL generation and stumbled upon the escort router which can also interface with express.










share|improve this question

























  • sure express can do a lot of stuff but there are a lot of frameworks built on top of express or even just on node.js itself. My personal favorite is Hapi.js. theres also sails. or just pure node if your crazy enough. i've used both of these in production environments. in the case of hapi what you are really probably looking for is url params. but your not limited there are tons of frameworks to pick from in node.

    – Chacliff
    Jan 9 at 4:02
















17















I'm considering using Express framework in my next node.js project. However, a stumbling block for me is non-existence of URL generation for routes like in most other non-Sinatra based frameworks, examples- Django, Flask, Rails etc.



I tried looking for some Connect middleware to serve my task and I did find Barista, Escort, Sherpa and the likes but looking at their GitHub pages, all seem dead and in-active. So, I don't want to go for something that's not maintained anymore for obvious reasons.



My main concern here is that the project may get really large and it WILL be a pain to update URLs in every page whenever business and/or aesthetic requirements change.



Is there something that I failed to see in the docs/tests? If not, then how do I extend the routing framework in Express to do URL generation and make this wrapper available in my views as well as controller functions?



UPDATE: (22/3/2012) I found this page: https://github.com/clyfe/tweet_express/wiki/TODO which specified some routers that do URL generation and stumbled upon the escort router which can also interface with express.










share|improve this question

























  • sure express can do a lot of stuff but there are a lot of frameworks built on top of express or even just on node.js itself. My personal favorite is Hapi.js. theres also sails. or just pure node if your crazy enough. i've used both of these in production environments. in the case of hapi what you are really probably looking for is url params. but your not limited there are tons of frameworks to pick from in node.

    – Chacliff
    Jan 9 at 4:02














17












17








17


1






I'm considering using Express framework in my next node.js project. However, a stumbling block for me is non-existence of URL generation for routes like in most other non-Sinatra based frameworks, examples- Django, Flask, Rails etc.



I tried looking for some Connect middleware to serve my task and I did find Barista, Escort, Sherpa and the likes but looking at their GitHub pages, all seem dead and in-active. So, I don't want to go for something that's not maintained anymore for obvious reasons.



My main concern here is that the project may get really large and it WILL be a pain to update URLs in every page whenever business and/or aesthetic requirements change.



Is there something that I failed to see in the docs/tests? If not, then how do I extend the routing framework in Express to do URL generation and make this wrapper available in my views as well as controller functions?



UPDATE: (22/3/2012) I found this page: https://github.com/clyfe/tweet_express/wiki/TODO which specified some routers that do URL generation and stumbled upon the escort router which can also interface with express.










share|improve this question
















I'm considering using Express framework in my next node.js project. However, a stumbling block for me is non-existence of URL generation for routes like in most other non-Sinatra based frameworks, examples- Django, Flask, Rails etc.



I tried looking for some Connect middleware to serve my task and I did find Barista, Escort, Sherpa and the likes but looking at their GitHub pages, all seem dead and in-active. So, I don't want to go for something that's not maintained anymore for obvious reasons.



My main concern here is that the project may get really large and it WILL be a pain to update URLs in every page whenever business and/or aesthetic requirements change.



Is there something that I failed to see in the docs/tests? If not, then how do I extend the routing framework in Express to do URL generation and make this wrapper available in my views as well as controller functions?



UPDATE: (22/3/2012) I found this page: https://github.com/clyfe/tweet_express/wiki/TODO which specified some routers that do URL generation and stumbled upon the escort router which can also interface with express.







node.js express






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 22 '12 at 10:11







Ishbir

















asked Jan 6 '12 at 7:30









IshbirIshbir

151211




151211













  • sure express can do a lot of stuff but there are a lot of frameworks built on top of express or even just on node.js itself. My personal favorite is Hapi.js. theres also sails. or just pure node if your crazy enough. i've used both of these in production environments. in the case of hapi what you are really probably looking for is url params. but your not limited there are tons of frameworks to pick from in node.

    – Chacliff
    Jan 9 at 4:02



















  • sure express can do a lot of stuff but there are a lot of frameworks built on top of express or even just on node.js itself. My personal favorite is Hapi.js. theres also sails. or just pure node if your crazy enough. i've used both of these in production environments. in the case of hapi what you are really probably looking for is url params. but your not limited there are tons of frameworks to pick from in node.

    – Chacliff
    Jan 9 at 4:02

















sure express can do a lot of stuff but there are a lot of frameworks built on top of express or even just on node.js itself. My personal favorite is Hapi.js. theres also sails. or just pure node if your crazy enough. i've used both of these in production environments. in the case of hapi what you are really probably looking for is url params. but your not limited there are tons of frameworks to pick from in node.

– Chacliff
Jan 9 at 4:02





sure express can do a lot of stuff but there are a lot of frameworks built on top of express or even just on node.js itself. My personal favorite is Hapi.js. theres also sails. or just pure node if your crazy enough. i've used both of these in production environments. in the case of hapi what you are really probably looking for is url params. but your not limited there are tons of frameworks to pick from in node.

– Chacliff
Jan 9 at 4:02












3 Answers
3






active

oldest

votes


















10














You might try Locomotive, which is built on Express.



It does much more than route generation. From the docs: "Locomotive brings additional MVC-based structure, for architecting larger applications, while leveraging the power of Express and Connect middleware."



Locomotive's router generates helpers that are automatically available to controllers and views.






share|improve this answer
























  • Locomotive seems pretty interesting.. thanks! :)

    – Ishbir
    Feb 14 '12 at 14:18






  • 2





    Note in 2019: Locomotive hasn't been updated since 2014.

    – Hendy Irawan
    Jan 2 at 6:50



















14














Or stick with express and use the package reversable-router.



Example from the readme:



app.get('/admin/user/:id', 'admin.user.edit', function(req, res, next){
//...
});

//.. and a helper in the view files:
url('admin.user.edit', {id: 2})





share|improve this answer



















  • 1





    FYI, the new maintained repo is here: github.com/alubbe/named-routes

    – mindeavor
    Jun 16 '15 at 16:00











  • Note in 2019: named-routes hasn't been maintained since 2016.

    – Hendy Irawan
    Jan 2 at 6:51



















0














From @freakish's answer:



There is no out of the box mechanism for that. However you can mimic Django's style like that: define urls.js file which will hold an array of URLs. First start with:



myviews.js



exports.Index = function( req, res, next ) {
res.send( "hello world!" );
};


urls.js



var MyViews = require( "mywviews.js" );

module.exports = [
{ name : "index", pattern : "/", view : MyViews.Index }
]


Now in app.js ( or whatever the main file is ) you need to bind urls to Express. For example like this:



app.js



var urls = require( "urls.js" );

for ( var i = 0, l = urls.length; i < l; i++ ) {
var url = urls[ i ];
app.all( url.pattern, url.view );
};


Now you can define custom helper ( Express 3.0 style ):



var urls = require( "urls.js" ), l = urls.length;
app.locals.url = function( name ) {
for ( var i = 0; i < l; i++ ) {
var url = urls[ i ];
if ( url.name === name ) {
return url.pattern;
}
};
};


and you can easily use it in your template. Now the problem is that it does not give you fancy URL creation mechanism like in Django ( where you can pass additional parameters to url ). On the other hand you can modify url function and extend it. I don't want to go into all details here, but here's an example how to use regular expressions ( you should be able to combine these to ideas together ):



Express JS reverse URL route (Django style)



Note that I posted the question, so I had the same problem some time ago. :D






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8754741%2furl-generation-for-routes-in-express%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    10














    You might try Locomotive, which is built on Express.



    It does much more than route generation. From the docs: "Locomotive brings additional MVC-based structure, for architecting larger applications, while leveraging the power of Express and Connect middleware."



    Locomotive's router generates helpers that are automatically available to controllers and views.






    share|improve this answer
























    • Locomotive seems pretty interesting.. thanks! :)

      – Ishbir
      Feb 14 '12 at 14:18






    • 2





      Note in 2019: Locomotive hasn't been updated since 2014.

      – Hendy Irawan
      Jan 2 at 6:50
















    10














    You might try Locomotive, which is built on Express.



    It does much more than route generation. From the docs: "Locomotive brings additional MVC-based structure, for architecting larger applications, while leveraging the power of Express and Connect middleware."



    Locomotive's router generates helpers that are automatically available to controllers and views.






    share|improve this answer
























    • Locomotive seems pretty interesting.. thanks! :)

      – Ishbir
      Feb 14 '12 at 14:18






    • 2





      Note in 2019: Locomotive hasn't been updated since 2014.

      – Hendy Irawan
      Jan 2 at 6:50














    10












    10








    10







    You might try Locomotive, which is built on Express.



    It does much more than route generation. From the docs: "Locomotive brings additional MVC-based structure, for architecting larger applications, while leveraging the power of Express and Connect middleware."



    Locomotive's router generates helpers that are automatically available to controllers and views.






    share|improve this answer













    You might try Locomotive, which is built on Express.



    It does much more than route generation. From the docs: "Locomotive brings additional MVC-based structure, for architecting larger applications, while leveraging the power of Express and Connect middleware."



    Locomotive's router generates helpers that are automatically available to controllers and views.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Feb 13 '12 at 6:34









    Luke VivierLuke Vivier

    31625




    31625













    • Locomotive seems pretty interesting.. thanks! :)

      – Ishbir
      Feb 14 '12 at 14:18






    • 2





      Note in 2019: Locomotive hasn't been updated since 2014.

      – Hendy Irawan
      Jan 2 at 6:50



















    • Locomotive seems pretty interesting.. thanks! :)

      – Ishbir
      Feb 14 '12 at 14:18






    • 2





      Note in 2019: Locomotive hasn't been updated since 2014.

      – Hendy Irawan
      Jan 2 at 6:50

















    Locomotive seems pretty interesting.. thanks! :)

    – Ishbir
    Feb 14 '12 at 14:18





    Locomotive seems pretty interesting.. thanks! :)

    – Ishbir
    Feb 14 '12 at 14:18




    2




    2





    Note in 2019: Locomotive hasn't been updated since 2014.

    – Hendy Irawan
    Jan 2 at 6:50





    Note in 2019: Locomotive hasn't been updated since 2014.

    – Hendy Irawan
    Jan 2 at 6:50













    14














    Or stick with express and use the package reversable-router.



    Example from the readme:



    app.get('/admin/user/:id', 'admin.user.edit', function(req, res, next){
    //...
    });

    //.. and a helper in the view files:
    url('admin.user.edit', {id: 2})





    share|improve this answer



















    • 1





      FYI, the new maintained repo is here: github.com/alubbe/named-routes

      – mindeavor
      Jun 16 '15 at 16:00











    • Note in 2019: named-routes hasn't been maintained since 2016.

      – Hendy Irawan
      Jan 2 at 6:51
















    14














    Or stick with express and use the package reversable-router.



    Example from the readme:



    app.get('/admin/user/:id', 'admin.user.edit', function(req, res, next){
    //...
    });

    //.. and a helper in the view files:
    url('admin.user.edit', {id: 2})





    share|improve this answer



















    • 1





      FYI, the new maintained repo is here: github.com/alubbe/named-routes

      – mindeavor
      Jun 16 '15 at 16:00











    • Note in 2019: named-routes hasn't been maintained since 2016.

      – Hendy Irawan
      Jan 2 at 6:51














    14












    14








    14







    Or stick with express and use the package reversable-router.



    Example from the readme:



    app.get('/admin/user/:id', 'admin.user.edit', function(req, res, next){
    //...
    });

    //.. and a helper in the view files:
    url('admin.user.edit', {id: 2})





    share|improve this answer













    Or stick with express and use the package reversable-router.



    Example from the readme:



    app.get('/admin/user/:id', 'admin.user.edit', function(req, res, next){
    //...
    });

    //.. and a helper in the view files:
    url('admin.user.edit', {id: 2})






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Oct 15 '12 at 9:52









    antitoxicantitoxic

    3,0322943




    3,0322943








    • 1





      FYI, the new maintained repo is here: github.com/alubbe/named-routes

      – mindeavor
      Jun 16 '15 at 16:00











    • Note in 2019: named-routes hasn't been maintained since 2016.

      – Hendy Irawan
      Jan 2 at 6:51














    • 1





      FYI, the new maintained repo is here: github.com/alubbe/named-routes

      – mindeavor
      Jun 16 '15 at 16:00











    • Note in 2019: named-routes hasn't been maintained since 2016.

      – Hendy Irawan
      Jan 2 at 6:51








    1




    1





    FYI, the new maintained repo is here: github.com/alubbe/named-routes

    – mindeavor
    Jun 16 '15 at 16:00





    FYI, the new maintained repo is here: github.com/alubbe/named-routes

    – mindeavor
    Jun 16 '15 at 16:00













    Note in 2019: named-routes hasn't been maintained since 2016.

    – Hendy Irawan
    Jan 2 at 6:51





    Note in 2019: named-routes hasn't been maintained since 2016.

    – Hendy Irawan
    Jan 2 at 6:51











    0














    From @freakish's answer:



    There is no out of the box mechanism for that. However you can mimic Django's style like that: define urls.js file which will hold an array of URLs. First start with:



    myviews.js



    exports.Index = function( req, res, next ) {
    res.send( "hello world!" );
    };


    urls.js



    var MyViews = require( "mywviews.js" );

    module.exports = [
    { name : "index", pattern : "/", view : MyViews.Index }
    ]


    Now in app.js ( or whatever the main file is ) you need to bind urls to Express. For example like this:



    app.js



    var urls = require( "urls.js" );

    for ( var i = 0, l = urls.length; i < l; i++ ) {
    var url = urls[ i ];
    app.all( url.pattern, url.view );
    };


    Now you can define custom helper ( Express 3.0 style ):



    var urls = require( "urls.js" ), l = urls.length;
    app.locals.url = function( name ) {
    for ( var i = 0; i < l; i++ ) {
    var url = urls[ i ];
    if ( url.name === name ) {
    return url.pattern;
    }
    };
    };


    and you can easily use it in your template. Now the problem is that it does not give you fancy URL creation mechanism like in Django ( where you can pass additional parameters to url ). On the other hand you can modify url function and extend it. I don't want to go into all details here, but here's an example how to use regular expressions ( you should be able to combine these to ideas together ):



    Express JS reverse URL route (Django style)



    Note that I posted the question, so I had the same problem some time ago. :D






    share|improve this answer




























      0














      From @freakish's answer:



      There is no out of the box mechanism for that. However you can mimic Django's style like that: define urls.js file which will hold an array of URLs. First start with:



      myviews.js



      exports.Index = function( req, res, next ) {
      res.send( "hello world!" );
      };


      urls.js



      var MyViews = require( "mywviews.js" );

      module.exports = [
      { name : "index", pattern : "/", view : MyViews.Index }
      ]


      Now in app.js ( or whatever the main file is ) you need to bind urls to Express. For example like this:



      app.js



      var urls = require( "urls.js" );

      for ( var i = 0, l = urls.length; i < l; i++ ) {
      var url = urls[ i ];
      app.all( url.pattern, url.view );
      };


      Now you can define custom helper ( Express 3.0 style ):



      var urls = require( "urls.js" ), l = urls.length;
      app.locals.url = function( name ) {
      for ( var i = 0; i < l; i++ ) {
      var url = urls[ i ];
      if ( url.name === name ) {
      return url.pattern;
      }
      };
      };


      and you can easily use it in your template. Now the problem is that it does not give you fancy URL creation mechanism like in Django ( where you can pass additional parameters to url ). On the other hand you can modify url function and extend it. I don't want to go into all details here, but here's an example how to use regular expressions ( you should be able to combine these to ideas together ):



      Express JS reverse URL route (Django style)



      Note that I posted the question, so I had the same problem some time ago. :D






      share|improve this answer


























        0












        0








        0







        From @freakish's answer:



        There is no out of the box mechanism for that. However you can mimic Django's style like that: define urls.js file which will hold an array of URLs. First start with:



        myviews.js



        exports.Index = function( req, res, next ) {
        res.send( "hello world!" );
        };


        urls.js



        var MyViews = require( "mywviews.js" );

        module.exports = [
        { name : "index", pattern : "/", view : MyViews.Index }
        ]


        Now in app.js ( or whatever the main file is ) you need to bind urls to Express. For example like this:



        app.js



        var urls = require( "urls.js" );

        for ( var i = 0, l = urls.length; i < l; i++ ) {
        var url = urls[ i ];
        app.all( url.pattern, url.view );
        };


        Now you can define custom helper ( Express 3.0 style ):



        var urls = require( "urls.js" ), l = urls.length;
        app.locals.url = function( name ) {
        for ( var i = 0; i < l; i++ ) {
        var url = urls[ i ];
        if ( url.name === name ) {
        return url.pattern;
        }
        };
        };


        and you can easily use it in your template. Now the problem is that it does not give you fancy URL creation mechanism like in Django ( where you can pass additional parameters to url ). On the other hand you can modify url function and extend it. I don't want to go into all details here, but here's an example how to use regular expressions ( you should be able to combine these to ideas together ):



        Express JS reverse URL route (Django style)



        Note that I posted the question, so I had the same problem some time ago. :D






        share|improve this answer













        From @freakish's answer:



        There is no out of the box mechanism for that. However you can mimic Django's style like that: define urls.js file which will hold an array of URLs. First start with:



        myviews.js



        exports.Index = function( req, res, next ) {
        res.send( "hello world!" );
        };


        urls.js



        var MyViews = require( "mywviews.js" );

        module.exports = [
        { name : "index", pattern : "/", view : MyViews.Index }
        ]


        Now in app.js ( or whatever the main file is ) you need to bind urls to Express. For example like this:



        app.js



        var urls = require( "urls.js" );

        for ( var i = 0, l = urls.length; i < l; i++ ) {
        var url = urls[ i ];
        app.all( url.pattern, url.view );
        };


        Now you can define custom helper ( Express 3.0 style ):



        var urls = require( "urls.js" ), l = urls.length;
        app.locals.url = function( name ) {
        for ( var i = 0; i < l; i++ ) {
        var url = urls[ i ];
        if ( url.name === name ) {
        return url.pattern;
        }
        };
        };


        and you can easily use it in your template. Now the problem is that it does not give you fancy URL creation mechanism like in Django ( where you can pass additional parameters to url ). On the other hand you can modify url function and extend it. I don't want to go into all details here, but here's an example how to use regular expressions ( you should be able to combine these to ideas together ):



        Express JS reverse URL route (Django style)



        Note that I posted the question, so I had the same problem some time ago. :D







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 2 at 6:55









        Hendy IrawanHendy Irawan

        12.6k87688




        12.6k87688






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8754741%2furl-generation-for-routes-in-express%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            The term 'EXEC' is not recognized as the name of a cmdlet Powershell

            NPM command prompt closes immediately [closed]

            Error binding properties and functions in emscripten