1

In Express 4, there is an object to list your app routes: app._router.stack

I try dumping it out to the console in a route in todos module routes file:

exports.update = (req,res) => {
  // console.log(app._router.stack);
  res.status(200).send({message:'TODO modify an existing post by using param ' + req.params.taskId, routes: app._router.stack});
};

On testing with Advance REST client. I saw my routes object:

"routes": [21]    
9:  {
"name": "session"
"params": {}
"path": ""
"keys": [0]
"regexp": {
"fast_slash": true
}-
}-
10:  {
"name": "<anonymous>"
"params": {}
"path": ""
"keys": [0]
"regexp": {
"fast_slash": true
}-
}-
11:  {
"name": "mounted_app"
"keys": [0]
"regexp": {}
}-
12:  {
"name": "bound dispatch"
"keys": [0]
"regexp": {}
"route": {
"path": "/home"
"stack": [1]
0:  {
"name": "<anonymous>"
"keys": [0]
"regexp": {}
"method": "get"
}-
-
"methods": {
"get": true
}-
}-
}-
13:  {
"name": "bound dispatch"
"keys": [0]
"regexp": {}
"route": {
"path": "/"
"stack": [1]
0:  {
"name": "<anonymous>"
"keys": [0]
"regexp": {}
"method": "get"
}-
-
"methods": {
"get": true
}-
}-
}-
14:  {
"name": "mounted_app"
"keys": [0]
"regexp": {}
}-
15:  {
"name": "mounted_app"
"params": {}
"path": "/todos"
"keys": [0]
"regexp": {}
}-
16:  {
"name": "mounted_app"
"keys": [0]
"regexp": {}
}-
17:  {
"name": "serveStatic"
"keys": [0]
"regexp": {
"fast_slash": true
}-
}

In my Express app, this is how my mounted apps were defined:

  if (module.re('core')){
    register(app);
  } else {
    var sub = express();
    register(sub);
    app.use('/' + module, sub);
  }


    function register(app){
    app.set('root', root);
    app.set('module_name', module);

    require(routes)(app);
    app.set('views', view_path);
    app.set('view engine', view_engine);
  }

For more details, please view my source

QUESTIONS:

  1. If you view my sources, I have several mounted apps and todos is just one of them. Why the other module/mounted apps are not listed in the routes object above? Why are there <anonymous> and empty mounted_app objects within the routes. Whether security loopholes or any other concern?

  2. How to list RESTful endpoints of the core, the todos app and other mounted_apps (preferably automatically) ? how to adjust my app design/architecture to facilitate the listing? I want to have an object of all routes, and so to be able to list core routes for main application navigation bar and mounted apps routes for child navigation bar.

Thank you very much.

EDIT: not duplicate, I saw that post about Express 4 routes with app._router.stack and went forward with my testing. Read my first line.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Pristine Kallio
  • 505
  • 5
  • 19
  • Review my first line and my Edit annotation. – Pristine Kallio Sep 02 '16 at 18:01
  • Your title is practically the same title as the dup so you're really asking for a dup there. If that isn't really what you're asking, then change your title to something else that is more descriptive or a better summary of the two actual questions you have listed at the end. If you've tried all the things in the dup and they don't answer your question, then you need to explain that and reference the specific dup so people know what that doesn't solve your question. – jfriend00 Sep 02 '16 at 18:13
  • No, it is for **mounted Apps** , and the words exist in the title. – Pristine Kallio Sep 02 '16 at 18:15
  • What do you mean "mounted apps"? There's no such term in Express lingo. There's an `app` object and there are routers. Not sure what a mounted app is. – jfriend00 Sep 02 '16 at 18:16
  • From object tree, "name": "mounted_app", this is what they (sub Apps) are defined and called in the `app._router.stack` – Pristine Kallio Sep 02 '16 at 18:18
  • OK, I still have no idea what you're actually asking for. But, I'll reopen. I think you should explain why all the techniques in that other answer don't do what you want. – jfriend00 Sep 02 '16 at 18:19
  • Ok, thank you very much. – Pristine Kallio Sep 02 '16 at 18:20
  • Wouldn't you have to fetch the mounted app objects and then iterate the routes on each of them? – jfriend00 Sep 02 '16 at 18:22
  • ok, I'm done with the issue. Thank you for your input. Wasn't sure how to export the new express() instance of the mounted apps but resolved as you said, with `moduls`object appending each `modul` (mounted app, renaming `module` to `modul` to avoid the reserved word) Releasing r34 of my source, will update answer to my question. – Pristine Kallio Sep 02 '16 at 19:17

2 Answers2

0

You can access all registered routes in an express app using the code provided here

Community
  • 1
  • 1
dmamills
  • 197
  • 1
  • 5
0

The following is to address my 2nd question. The first question is still pending assistance from you.

I created an extra array called moduls and basically with modul being an iterative module name, I am able to assign moduls[modul] = express() app instance.

+    moduls[modul] = express();
 +    var sub = moduls[modul];
      register(sub);

in other words, a mounted app is registered into moduls array, which later all I can loop through it with _.forOwn and assign the app._router.stack object to a new array called apps.

+  _.forOwn(moduls, function(val,key){
 +     apps[key] = val._router.stack;
 +  });
 +
 +  app.set('apps', apps);

Then, you can call 'apps' anywhere you like. In my git repo, I chose to load it in a route to view the response sent to an API client.

If you can, please help address my concern in the question 1.

Pristine Kallio
  • 505
  • 5
  • 19