Express dynamic routing using / and /:slug as different route files

so I'm in the process of refactoring my node/express application and I'm trying to separate my roots. Here's my issue:

I want an homepage and then a completely different page for extensions, that doesn't fall into other routes.

So for example, let's say I have 3 pages: Homepage, About, and then an user page where the URL is "mysite.com/username123".

Now I know (or at least believe) this can be accomplished by doing something like:

var express = require('express'); var router = express.Router(); router.get('/:slug', function(req, res, next) { if(req.params.slug) { var data = { my: 'data' }; res.render('index', data); } else { var userdata = { my: 'data' }; res.render('user', userdata); } }); module.exports = router;

But this isn't what I want to do; I would like to keep these in two different route files. So my app.js file would have something like:

var routes = require('./routes/index'); var vimeo = require('./routes/users'); var about = require('./routes/about'); app.use('/', index); app.use('/', users); app.use('/about', about);

Where /users renders if there is a slug. This actually sort of works. If there is no :slug, the index file is loaded, otherwise the users file is loaded (which has the :slug in the route file, not shown here). But then /about is overwritten.

Now I supposed I could shove /about to the top of the list and that would work, but this seems extremely sloppy and the whole point of doing this is to properly structure my code.

How are situations like this supposed to be properly handled? Could someone please give me some help here?

--------------Solutions-------------

It happens because /about is overridden by /:slug. In other words, express cannot tell if about is not meant to be a slug.

Solutions would be to either

  1. Add a url params to let differentiate the routes.

    router.get('/users/:slug')

  2. Skip to next route

    router.get(':slug', function (req, res, next) {`
    if (req.params.slug === 'about') {
    return next();
    }

    // get user data and render
    res.render('user', userdata);
    });

Attach the /about route below this.

Category:javascript Time:2018-12-31 Views:2

Related post

  • ASP.Net MVC Routing - using same pattern for different controllers 2009-07-24

    Say I have an ASP.Net MVC site with products and categories, and I want these urls... Products: Fruitshop.com/super-tasty-apple Fruitshop.com/squishy-orange Fruitshop.com/worm-infested-apple Fruitshop.com/megaorange Categories: Fruitshop.com/apples F

  • using a function in different .c files (c programming 101) 2011-08-16

    /me/home/file1.c containes function definition: int mine(int i) { /* some stupidity by me */ } I've declared this function in /me/home/file1.h int mine(int); if I want to use this function mine() in /me/home/at/file2.c To do so, all I need to do is:

  • How to use one icon from different image files? 2011-11-08

    is the set of icons within one image file. And is another set. How can I create a link (a href) with usage of icon from one file, on hover it should take icon from another file (the same position) and on click - from third file? Here is the CSS just

  • Mercurial. Version control and deployment. Different config files. How to? 2012-01-02

    I have a setup as follows. A private repository at bitbucket where I keep the 'master' repository. A repository on my server which acts as the 'live' website. A repository on my laptop which acts as my working copy. My process is as follows. I make a

  • use one macro in different excel files 2012-02-28

    I wrote a macro, which I would like to use in different excel files, which have almost the same table structure but different data. So is it possible anyhow just to "include" my macro-script to any excel file?! I read already this tip but it sounds f

  • What is Wordpress' routing architecture and how to trace a file by URL 2012-04-02

    What is Wordpress' routing architecture and how do you trace a file by URL? How would you go about routing a URL to a specific PHP file or PHP function. e.g. www.my-site.com/abc executes function xyz_function() in file xyz_file.php? --------------Sol

  • Netgear wireless router - Wireless and internet through the router drop repeatedly 2014-03-06

    I just bought a Netgear wireless router (model WNR2000). It seemed to work fine at first, then I set up parental controls for the routher. Since then the wireless and internet access shut off periodically and do not necessarily come back on without t

  • Use PHP method from different class/file 2011-12-06

    If I want to add methods to my file from other classes or a script not using OOP should I include() the file or extend it in my class? --------------Solutions------------- Include and create a new instance of this class. For OOP classes you would hav

  • Rails RESTful Routing With '/' and Slugs 2009-12-09

    I am looking to do something similar a wordpress slug where I have a URL like this while maintaining RESTful routing: http://foo.com/blog/2009/12/04/article-title The reason I am interested in keep RESTFUL routing is that I am unable to use many plug

  • spring beans and sessionFactory in different xml files 2008-11-03

    We have 3 applications using 3 different spring configuration files. But we have one database and one datasource, so one session factory.Hhow can we import the session factory bean into the 3 different spring config files? --------------Solutions----

  • link_to project index possible with customized routes.rb and without a Named route in Rails? 2010-10-05

    my routes.rb looks like this map.resources :bookmarks map.connect ':controller/:id/:action' map.connect ':controller/:action' so i can use urls like this http://localhost:3000/bookmarks/Ruby/show but when i try to link from this site to the index sit

  • Use same ComplexType in different WSDL files 2011-01-06

    I would like to use same ComplexType in two different WSDLs. How to define and include these ComplexTypes so i can use it in both WSDLs? (and practically in case of Java they are generated to the same package/datatype) --------------Solutions--------

  • Using reference passed from different cpp file in native code gives segmentation fault 2011-03-30

    I've tried this : calling a function (with parameter reference) from another cpp file, I can't assign anything to the parameter passed by reference. in a function Java_com_example_afeTest_testReference in test.cpp std::string a =""; const std::string

  • How to use same panel from different html files in jQueryMobile 2013-08-30

    Is it possible to use a panel defined, in index.html in another page, e.g., resutls.html ? Or do I need to define the panel in every page, and add the same html code in both pages ? Since I want my panel to be same in all pages. This is my panel in i

  • Compiling the same Xcode project for iPhone and iPad with different xib files 2010-09-21

    I would like to know if it's possible to compile the same Xcode project for both iPhone and iPad, automatically changing the xib file according to the platform. The project will contain both xib files. If this is possible, how can be done? Thanks ---

  • how to call sql server express command line using c# to run a .sql file 2012-02-01

    i have to make a setup for window application so i need to create a database on clinet machine .so i decided that i will create it using .sql file which i will add in my resource file.as there is a option to execute that file on first run of my progr

  • Which system repair should I use, and will it delete my files> 2012-04-04

    My Toshiba runs Windows 7 Home Premium 32 bit, and two days ago it refused to get much past the login screen, once I login it will almost instantly freeze and not respond. I'm thinking I should create a system repair disk, and run a startup repair. I

  • Can't connect through router using wireless, connection shows "local only" 2014-03-04

    I have same issue everyone else here is reporting. Can't connect through the router to internet. Shows "local only". Network shows up as "unidentified network". Keeps reverting to "public" even after I change it. Have always been able to connect thro

  • Illegal anotation exception in jaxb using substitution in two different file 2014-08-05

    When I am using substution in two different xsd file then JAXB throws error: First xsd RegTypy.xsd contains: <xs:element name="AisSeznamUdajuAbstract" abstract="true"/> <xs:element name="AisSeznamUdaju" type="AisSeznamUdajuType" substitution

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.481 (s). 13 q(s)