Search code examples
javascriptregexmeteorroutesiron-router

Regex routing with Iron-router


I'm trying to write a route with iron-router for meteor to catch all requests to:

  • /game/Quarterfinal3
  • /game/Semifinal5
  • /game/Grandfinal1

etc. This is what i tried, but it doesn't work...

Router.route('final', {path: "(\\/)(game)(\\/).*?(final)(\\d)"}, function() {
  console.log("final");
  this.render('notStartedGame');
});

How do I solve this?


Solution

  • You don't need regex in your path as long as you use :pathParam format on your paths and it will be available within this.params.pathParam, namely:

    if you are trying to catch those three routes:

    Router.route('/game/:gameName', function() {
      var acceptablePaths = ["Quarterfinal3", "Semifinal5", "Grandfinal1"],
          gameName = this.params.gameName;
      // check if the game name is one of those that we are looking for
      if ( _.contains(acceptablePath, gameName) ) this.render("notStartedGame");
      // otherwise render a not found template or if you want do something else
      this.render("gameNotFound"); //assuming you have such a template
    });
    

    or if you are looking for any route that contains "final" in it:

    Router.route('/game/:gameName', function() {
      var checkFor = "final",
          gameName = this.params.gameName;
      // check if the game name includes "final"
      if ( gameName.indexOf(checkFor) > -1 ) this.render("notStartedGame");
      // otherwise render a not found template or if you want do something else
      this.render("gameNotFound"); //assuming you have such a template
    });