2

I'm using express-generator.

The following codes are in app.js

1- The following two routings:

 **app.use('/', indexRouter);
app.use('/users', usersRouter);**

2- Andthe following are defined

  **var indexRouter = require('./routes/index');
  var usersRouter = require('./routes/users');**

The following codes are modules: 1- users.js with the following code

 **var express = require('express');
 var router = express.Router();
 /* GET users listing. */
 router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});
module.exports = router;**

2- index.js with the following code

  **var express = require('express');
  var router = express.Router();
  /* GET home page. */
  router.get('/', function (req, res, next) {
  res.render('index', {
     title: 'Express',
     condition: true
  });
  });
  module.exports = router;**

My question: if I hit the following URL (http://localhost/) then the
app.use('/', indexRouter); shall be called . and when I hit (http://localhost/users) the same URL shall be called which is app.use('/', indexRouter); because use finds the first matched pattern which it is / . But it calls app.use('/users', usersRouter) instead.

If it's get instead of use then its OK and making sense for me, could anybody explain this code behavior? thnx

  • Maybe check the answer on this link: https://stackoverflow.com/questions/11321635/nodejs-express-what-is-app-use – fedeteka May 24 '20 at 01:48

0 Answers0