Is it a good idea to create all the routes when my app starts?

I use Node.js in my new web application, and I create all routes when my application starts. Does this affect my application in a positive or negative way?

For example, I have several routes:

app.get('admin/1', controller.1);
app.get('admin/2', controller.2);
app.get('admin/3', controller.3);
app.get('admin/4', controller.4);
app.get('admin/5', controller.5);

When the user requests a route, the controller will be loaded, right?

So, I made this little function that will create all the routes when the application starts.

The code above will look like this:

registerRoute(app,
    [
        ['GET', '/admin/1', controller.1],
        ['GET', '/admin/2', controller.2],
        ['GET', '/admin/3', controller.3],
        ['GET', '/admin/4', controller.4],
        ['GET', '/admin/5', controller.5],
    ]
);

I think this is more organized, but I want this to affect my application.

Thank!


Update:

Example:

registerRoute(app,
    [
        ['GET', '/admin', mainController.index],
        ['GET', '/admin/events', eventController.index],
    ]
);

And registerRoute code:

var registerRoute = function (app, arr) {
    for (var i = 0; i < arr.length; i++) {
        createRoute(app, arr[i]);
    }
};

function createRoute(app, arr) {
    if (arr[0] === 'GET') {
        createGET(app, arr);
    }
    ...
}

function createGET(app, arr) {
    app.get(arr[1], arr[2]);
}

module.exports = registerRoute;

This is an example of the verb GET.

+4
source share
1 answer

... . . , , ... , .

+1

All Articles