Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is `_router` in express?

Tags:

express

I'm new to express and often see the code like this:

app.once('mount',function onmount(parent){
    parent._router.stack.pop();
});

Not sure what is _router and why they pop it? Any reasons behind it?

like image 722
batman Avatar asked Sep 18 '25 19:09

batman


1 Answers

The mount event occurs when a sub-app is registered with a parent app. The parent argument is the parent app object.

parent._router is the router associated with that parent app object.

parent._router.stack is the array of routes registered with that route.

parent._router.stack.pop() is removing the last registered route from that router.

There isn't enough context here for us to know why that last route is being removed. It's possible they are trying to remove the 404 error route (just a guess).

FYI, this direct manipulation of private instance variables is not documented behavior.

like image 171
jfriend00 Avatar answered Sep 23 '25 11:09

jfriend00