Reputation: 1696
I create a sub route in my server like '/users'
and it uses userRoute = express.Router()
But in express document in mountpath part it use another way like a use userRoute = express()
for sub route and call it sub app here it is:
var app = express(); // the main app
var admin = express(); // the sub app
...
app.use('/admin', admin); // mount the sub app
What are their difference and usage?
Upvotes: 2
Views: 221
Reputation: 303
You always need to use express()
to create the top-level server app, but to create sub-apps containing isolated blocks of routes or other functionality you can choose between mounting a new express()
app, or an express.Router()
Router.
The difference between these is in the amount of specific functionality provided to that block; Routers are simpler and focus mainly only on routing, and may be enough in many cases where you just want to logically organise your application. If you look at the the documented properties, methods & events available to both the Application and Router objects, you see that Application has all the ones that Router does, with additional ones that can be grouped into four main areas of functionality:
get()
, set()
, disable()
, disabled()
, enable()
, enabled()
)engine()
, render()
, locals
)mount
event)mountpath
, path()
)So if you don't need to use any of these, or in some cases do but don't need them to be isolated from the parent app, then you can use a Router.
Upvotes: 1
Reputation: 1696
Thanks to jfriend00 when I use an app
instead of router
I can set specific theme engine or ... for my route.
Upvotes: 0