How the Express Router Function Works

2

To use middleware in Express, I type the code below:

var express = require('express');
var app = express();

app.use('/', function(req, res, next) { 
    console.log('Middleware!');
});

However, I realized that it is also possible to do this:

var express = require('express');
var router = express.Router();

var app = express();

router.get('/', function(req, res, next) {
    res.send('Index!');
});

router.get('/teste', function(req, res, next) {
    res.send('Teste!');
});

app.use('/', router);

How does the Router function work? What does she do under the covers?

    
asked by anonymous 01.05.2016 / 16:42

0 answers