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:
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?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.