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;
1
  • app.get shouldn't be used in conjunction with a router given the router may have multiple routes using different verbs. Commented Feb 14, 2019 at 0:18

1 Answer 1

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.',
}));
Sign up to request clarification or add additional context in comments.

12 Comments

why only " app.get('/users', usersRouter)" is not working?
You should pass a function to the app.get, like: app.get('/users', getUsers)
I have updated the answer and included a working GET request on the route /users
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.
I tried it from your answer. And It works But why app.get('/', indexRouter) is working? indexRouter is also not function
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.