1
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

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

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));


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

module.exports = app;

I created express-generator. Then I tried to change app.use('/', indexRouter)app.get('/', indexRouter) and app.use('/users', usersRouter)app.get('/users', usersRouter).

Then app.get('/', indexRouter) was working(can hit the URL and get the page information). But app.get('/users', usersRouter) was NOT working. This returned 404 (NotFoundError: Not Found).

I already read this questionDifference between app.use and app.get in express.js But I couldn't understand

./routes/index.js

 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;

./routes/users

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;
Kazu25
  • 87
  • 1
  • 2
  • 13
  • `app.get` shouldn't be used in conjunction with a router given the router may have multiple routes using different verbs. – James Feb 14 '19 at 00:18

1 Answers1

1

app.use is designed for middlewares and app.get is designed for GET requests. Middlewares are functions that are called before the controller. You may have a middleware to check if the user is authenticate or not, and accept the user's request or deny it.

When you have app.use('/', indexRouter), the indexRouter will be called for all your requests on all your routes. It is like /*.

When you have app.use('/users', usersRouter), the usersRouter will be called for all your requests on all routes that start with /users. It is like /users*.

This is how you can do a GET request:

app.get('/users', (req, res) => res.status(200).send({
  message: 'It works.',
}));
vmf91
  • 1,897
  • 19
  • 27
  • why only " app.get('/users', usersRouter)" is not working? – Kazu25 Feb 14 '19 at 01:07
  • You should pass a function to the `app.get`, like: `app.get('/users', getUsers)` – vmf91 Feb 14 '19 at 01:09
  • I have updated the answer and included a working GET request on the route `/users` – vmf91 Feb 14 '19 at 01:10
  • When you configure an `app.get` you should pass a route and a function to be called when the route is accessed via GET. Copy the `app.get` from the answer and try it out. – vmf91 Feb 14 '19 at 01:15
  • I tried it from your answer. And It works But why `app.get('/', indexRouter)` is working? `indexRouter` is also not function – Kazu25 Feb 14 '19 at 01:19
  • What is the content of this file >> `./routes/index` ? – vmf91 Feb 14 '19 at 01:20
  • I uploaded ./routes/index in question. please check – Kazu25 Feb 14 '19 at 01:26
  • Oh, now I got it. `indexRouter` will be called for every request. If the request matches any of the `app.get` inside the `indexRouter`, the matched will be called as well. – vmf91 Feb 14 '19 at 01:29
  • I can't understand what you mean – Kazu25 Feb 14 '19 at 01:32
  • Even when you do a GET request on your `/users`, the `indexRouter` will be checked if any of its routes matches the `/users`. Because `indexRouter` is a middleware on the root `/`. – vmf91 Feb 14 '19 at 01:37
  • I understand it from you and this [link](https://stackoverflow.com/a/50582736/10145453).I appreciate you – Kazu25 Feb 14 '19 at 01:52
  • It may be useful to add app.get(/, ..) before static middleware, then add app.get(/*, ..) after – teaseaque Aug 30 '23 at 01:57