Why / are users being routed to Express and Node?
Edit: Default Express App:
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var routes = require('./routes/index');
var users = require('./routes/users');
----------------------------------------
These refer to files that look like:
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res) {
res.render('index', { title: 'Express' });
});
module.exports = router;
------------------------------
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/users', users);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;
However, the documentation says:
// this middleware will not allow the request to go beyond it
app.use(function(req, res, next) {
res.send('Hello World');
})
// requests will never reach this route
app.get('/', function (req, res) {
res.send('Welcome');
})
So my question is, why did the request ever make its way to a route /users
when the default route (i.e. '/'
) is already specified? Is it because routes
u users
are not functions?
In a related note, why even point /
out if this is what is used by default every time?
Finally, the application specifies the route by default '/users'
: why not just put another path in the route index.js
? I am confused as to how the application can specify app.use('/users', users)
and then in the user route specify
router.get('/', function(req, res) {
res.send('respond with a resource');
});
What matches /
? It seemed like all requests /
would be handled by the first route (the one using the routes
default file )
source to share
app.use()
is middleware. You pass it an additional path and function, and it's a function job to decide if it wants to pass the request to additional middleware or further routes. He does this by calling next()
or if he does not want to pass it, he does not call next()
.
So, if you have:
app.use("/", fn);
This middleware will be called for all paths, but the code inside the function you are passing determines whether the request should be passed or not.
source to share
The Express 4 application framework has two routers: routes
(installed on '/'
) and users
(installed on '/users'
). You can use both of them, or only routes
, or you can even add more routers.
app.js:
var routes = require('./routes/index');
var users = require('./routes/users');
app.use('/users', users); // please note the mounting point!
app.use('/', routes);
users.js
var express = require('express');
var router = express.Router();
router.get('/', function(req, res, next) {
res.send('respond with a resource');
});
module.exports = router;
Note that router.get('/', ... )
for the router it users
means the requested url http://yourserver/users
and nothttp://yourserver/
source to share