Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

440
Visualizações
What's the difference between application- and router level- middleware when routing in Express?

In the Express docs, it says:

Application level middleware are bound to an instance of express, using app.use() and app.VERB().

Router level middleware work just like application level middleware except they are bound to an instance of express.Router(). The middleware system created at the application level in the example above, can be replicated at the router level using the following code.

In the app provided by the Express generator, I see in the main app.js, there is:

var routes = require('./routes/index');
app.use('/', routes);

And in ./routes/index.js, I see this:

var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

module.exports = router;

What is the purpose of app.use passing it to router.get instead of simply using app.get? And generally, what's the difference between app.VERB and router.VERB in terms of routing?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

What is the purpose of app.use passing it to router.get instead of simply using app.get?

This is all just designing for modularity. It allows an application to be divided into several smaller loosely-coupled pieces and because they all do not have any direct knowledge of the shared app instance that combines them together, the modularity is achieved.

For example, you could build a whole user account subsystem that handles sign up, login, forgot password, etc, and share it between several different applications by "mounting" it within them via app.use(require("my-user-system")).

That's the only purpose behind this. There no functional, technical, or performance difference otherwise.

And generally, what's the difference between app.VERB and router.VERB in terms of routing?

No difference. The app has it's own main/primary router and app.VERB is just convenience sugar for the equivalent of app.router.VERB.

over 4 years ago · Santiago Trujillo Relatório

0

An example will help here:

In the birds.js:

// birds.js

var express = require('express');
var router = express.Router();

// middleware that is specific to this router
router.use(function timeLog(req, res, next) {
    console.log('Time: ', Date.now());
    next();
});
// define the home page route
router.get('/', function (req, res) {
    res.send('Birds home page');
});

In the app.js:

// app.js

const express = require('express')
const app = express()

var birds = require('./birds');
app.use('/birds', birds);

app.listen(3000, () => console.log('app listening on port 3000!\naccess http://localhost:3000/birds/'))

Now requests to http://localhost:3000/birds/ will go to birds.js file.

This modular approach will make easy to maintain a large code base, as the application grows big.

Source: https://expressjs.com/en/guide/routing.html

over 4 years ago · Santiago Trujillo Relatório

0

Application has the method listen and some other which router doesn't have.

Application "does" the main thing, while route only groups some routes, provides encapsulation (see Manohar's answer with example).

Routes in router doesn't necessarily know in advance what will be its full route. See in the example that '/birds' is defined in app.js which is root for router in birds.js ... difference between app.VERB and rourer.VERB is in the context.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda