Startec Startec - 1 year ago 83
Node.js Question

Why the /users route in Express and Node?

Edit: The default express app is this:

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(bodyParser.urlencoded({ extended: false }));
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;

// 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) {

So my question, is why would a request ever make its way to the
route when a default (i.e.
) route has already been specified? Is it because
are not functions?

On a related note, why even specify the
if that is what is used by default each time?

Lastly, the default app specifies the
route: Why not just put another path in the
route? I am confused how the app can specify
app.use('/users', users)
and then, in the users route, specify

router.get('/', function(req, res) {
res.send('respond with a resource');

What does the
correspond to? It seemed like all requests to
would be handled by the first route (the one that use the
default file)

Answer Source

app.use() is middleware. You pass it an optional path and a function and it is the function's job to decide if it wants to pass the request on to further middleware or further routes. It does that by calling next() or if it doesn't want to pass it on, it doesn't call next().

So, if you have:

app.use("/", fn);

That middleware will get called for all paths, but the code inside the function you pass it decides whether to pass the request on or not.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download