Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between express.Router and app.get?

I'm starting with NodeJS and Express 4, and I'm a bit confused. I been reading the Express website, but can't see when to use a route handler or when to use express.Router.

As I could see, if I want to show a page or something when the user hits /show for example I should use:

var express = require('express')     var app = express()     app.get("/show", someFunction)   

At the beginning, I thought this was old (for Express 3). Is that right or this is the way for Express 4 too?

If this is the way to do it in Express 4, what is express.Router used for?

I read almost the same example as above but using express.Router:

var express = require('express'); var router = express.Router(); router.get("/show", someFunction) 

So, what's the difference between both examples?

Which one should I use if I just want to do a simple testing website?

like image 816
nelson687 Avatar asked Feb 03 '15 17:02

nelson687


People also ask

What is difference between app and router in Express?

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.

What is the difference between app use and router use?

Show activity on this post. router. use(); mounts middleware for the routes served by the specific router, app. use(); mounts middleware for all routes of the app (or those matching the routes specified if you use app.

What does Express router do?

The express. Router() function is used to create a new router object. This function is used when you want to create a new router object in your program to handle requests.


1 Answers

app.js

var express = require('express'),     dogs    = require('./routes/dogs'),     cats    = require('./routes/cats'),     birds   = require('./routes/birds');  var app = express();  app.use('/dogs',  dogs); app.use('/cats',  cats); app.use('/birds', birds);  app.listen(3000); 

dogs.js

var express = require('express');  var router = express.Router();  router.get('/', function(req, res) {     res.send('GET handler for /dogs route.'); });  router.post('/', function(req, res) {     res.send('POST handler for /dogs route.'); });  module.exports = router; 

When var app = express() is called, an app object is returned. Think of this as the main app.

When var router = express.Router() is called, a slightly different mini app is returned. The idea behind the mini app is that each route in your app can become quite complicated, and you'd benefit from moving all that code into a separate file. Each file's router becomes a mini app, which has a very similar structure to the main app.

In the example above, the code for the /dogs route has been moved into its own file so it doesn't clutter up the main app. The code for /cats and /birds would be structured similarly in their own files. By separating this code into three mini apps, you can work on the logic for each one in isolation, and not worry about how it will affect the other two.

If you have code (middleware) that pertains to all three routes, you can put it in the main app, before the app.use(...) calls. If you have code (middleware) that pertains to just one of those routes, you can put it in the file for that route only.

like image 77
Nocturno Avatar answered Oct 07 '22 05:10

Nocturno