Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8258373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:31:04+00:00 2026-06-08T02:31:04+00:00

I am creating a reusable node.js NavigationController class so I can reuse this in

  • 0

I am creating a reusable node.js NavigationController class so I can reuse this in other server side projects If I may need or someone else may find it useful.

Here’s the use case.

var navController = new NavigationController({
    routes : {
        '/user/:action/:anything' : 'UserController',
        '/app/:action' : 'AppController',
        '/file/:action' : 'FileController',
        '/feedback/:action' : 'FeedbackController',
        '/:anything' : 'ErrorController'
    },
    ErrorController : 'ErrorController'
});
navController.init();

The user upon server request can call this function of that object.

navController.navigate(req, res);

Now this and controllers are being called rightly. The thing under navigate(req, res) function which is a part of calling appropriate controller object based on URL is defined as function named getRouteByPath(path). This private function will get the route and will allow navigate(req, res) function to get controller class name to call.

var getRouteByPath = function(path) {
    for(var route in config.routes) {
        var routeRegex = '';

        var routeParts = route.split('/');

        for(var rp = 0; rp < routeParts.length; rp++) {

            // checking if route part starts with :
            if(routeParts[rp].indexOf(':') === 0) {

                // this is "anything" part
                routeRegex += '[/]?([A-Za-z0-9]*)';

            } else if(routeParts[rp] != "") {
                routeRegex += '[/]?' + routeParts[rp];
            }
        }

        var routeRegexResult = path.match(routeRegex);
        if(routeRegexResult) {
            console.log(routeRegexResult);
            return route;
        }
    }
    return null;
};

I am too worried about this function as if this is the right way to do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T02:31:05+00:00Added an answer on June 8, 2026 at 2:31 am

    Some flaws:

    • Why do you use the slash as a character class ([/])? No need to do that, only in regex literals you’d need to escape it with a backslash (like /\//g). Just use a single “/” instead (new RegExp("/", "g")).

    • .indexOf(<string>)==0 does work, but searches the whole string and is not very efficient. Better use startswith, in your case routePart.charAt(0)==":"

    • <string>.match(<string>) – I recommend building a new RegExp object and use .test, as you don’t want to match – there is also no need to build capturing groups, I think, because you only return the route string but no matches (okay, you log them).

    • You want to check if the whole path matches your regexp? Dont forget to add ^ and $. Your current regex for the AppController also matches a route like /user/app/example.

    • Why is your slash (and only the slash) optional (/?)? Not only I don’t think this is what you want, but it also opens the doors to catastrophic backtracking when building regexes like /\/?user\/?([A-Za-z0-9]*)\/?([A-Za-z0-9]*)/

      To escape that, you would need to make the whole group optional: (?:/([^/]*))?.

    • Also, you should build the regexes only once (on initialisation) and store them in a cache, instead of building them each time you call getRouteByPath. The compilation of the RegExp is somewhere hidden in your code, although it needs to happen.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I'm creating reusable UI components that can be easy instantiated later, eg: this.emailInput
I need to create reusable Devise extension. This is my first experience of creating
I am creating reusable Data Access Layer for SQL Server for some projects. I
Creating a server-side socket will fail if I'm trying to use the same port
Are there any resources with information creating a self contained, reusable module? I need
I'm creating a generic reusable UIViewController component that people can add to their applications.
Creating a class at runtime is done as follows: klass = Class.new superclass, &block
(creating a separate question after comments on this: Javascript redeclared global variable overrides old
Error creating bean with name 'sessionFactory' defined in class path resource [ApplicationContext.xml]: Invocation of
I was working on creating a reusable custom control (not a user control) in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.