Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Express, what does app.router do exactly?

When I create a sample Express application using the express binary, the bootstrap code has these lines:

...  var app = express(); ... app.use(app.router); 

I didn't find much about app.router. I thought that this is the middleware that handles the routing (app.get(), app.post() etc.) rules, but these rules also get executed when I remove the app.use(app.router); line.

So what is the exact purpuse of this middleware?

like image 983
Zsombor Erdődy-Nagy Avatar asked Nov 06 '12 15:11

Zsombor Erdődy-Nagy


People also ask

What is app router in Express?

express. Router() is use multiple times to define groups of routes. route used as middleware to process requests. route used as middleware to validate parameters using ".

What does app use router do?

app. use() used to Mounts the middleware function or functions at the specified path,the middleware function is executed when the base of the requested path matches path. router. use() is used to middleware function or functions, The defaults mount path to “/”.

How do Express routers work?

We can create separate files and import them but express gives a router mechanism which is easy to use. router. get('/add-username', (req, res,next)=>{ res. send('<form action="/post-username" method="POST"> <input type="text" name="username"> <button type="submit"> Send </button> </form>'); }); router.

What is the difference between app and router?

Router class can be used to create modular mountable route handlers. A Router instance is a complete middleware and routing system; for this reason it is often referred to as a "mini-app"." Possible duplicate of What is the difference between "express. Router" and routing using "app.


2 Answers

In Express 3.x, app.router is an enhanced version of the connect middleware router. As hector said, this is where Express handles the request handlers registered with app.get, app.post, etc.

If you do not call app.use(app.router) explicitly then express will call it implicitly the first time you use app.get(...), app.post(...), etc. However, you may want to .use it explicitly, because then you choose the order of all your middleware.

app.use(express.favicon()); app.use(express.bodyParser()); app.use(express.methodOverride()); // app.get, app.post, etc called before static folder app.use(app.router);  app.use(express.static(path.join(__dirname, 'public'))); 

See how the router is retrieved in the Express 3 source here.

Note that Express 4 doesn't need app.router.

like image 84
Charles Holbrow Avatar answered Sep 30 '22 21:09

Charles Holbrow


This is from the Express 2.x guide http://expressjs.com/2x/guide.html

"Note the use of app.router, which can (optionally) be used to mount the application routes, otherwise the first call to app.get(), app.post(), etc will mount the routes."

I suspect this applies to Express 3.x too.

like image 27
Hector Correa Avatar answered Sep 30 '22 22:09

Hector Correa