I’m trying to setup an MVC architecture for Express. What I am trying to accomplish is a routing mechanism close to ASP.NET’s. For example for the following route:
/users/detail/1
express should call a module under controllers directory named users.js. Within the users.js module is a function named detail. And within the function, I can simply get the request parameter to get the id of the user.
My idea is to extract the users and map it to a users.js file using a simple require statement. But how can I tell express to call details() function by simply extracting the action part of the route which is ‘detail’ in the above example. I can use eval() but I am hearing that it’s not a safe thing to do? Thanks in advance.
In browser-side javascript, you can typically do the following
You can do similar in node by replacing
windowwithglobalsuch asHowever, if you are pulling this function in from another file, it will be little different. Let’s assume that you have the following file
a_module.js:And then in you’re main file, you can do the following: