Secondary routes (nested routers) in Express


First, a little background and context :

I am developing an application with nodejs + express .

I am new to this technology, and the application will be of some complexity. So I'm doing it for modules , each on a specific route.


After a little more trasteo , I'm 100% that the bug is in how I use the routes; edito, to shorten it a bit and make it more concrete.

For an AJAX request to this URL


POST http://localhost:8080/auth/allowed

I have the following code (it's an extract, the real one is bigger, only what I think is relevant):

// main.js

const AuthModule = require( './auth' ); '/auth/*', AuthModule );

// Archivos estáticos. Sin autorización.
app.get( '*', EX.static( join( __dirname, 'static' ), {
  dotfiles: 'ignore',
  index: 'false',
  redirect: false
} ) );

app.listen( 8080 );

Where all the modules load correctly.



Located in the subdirectory auth , with 2 files:

// index.js

'use strict';

const EX = require( 'express' );

const AllowedModule = require( './allowed' );

const Router = EX.Router( );

Router.use( function( req, res, next ) { require( 'console' ).log( 'Módulo AUTH' ); next( ); } ); '/allowed', AllowedModule );

module.exports = Router;


// allowed.js

'use strict';

function Allowed( req, res ) {
  res.json( {
    lastupdate: currTime,
    users: [
      [ 'Usuario número 1', 1 ],
      [ 'Usuario número 2', 2 ],
  } );

  res.end( );

module.exports = Allowed;

Now, what happens: when I make a request for the file /login.html , it performs an AJAX POST /auth/allowed .

I have established dump points to console, which shows that I enter the file auth/index.js . From that file, you should call the middleware auth/allowed.js . However, in the browser I get a nice code 404 .

I understand that, for some reason, from the file auth/index.js is not called to my middleware auth/allowed.js ; instead, we continue with the primary route table (the one I set in main.js ). The call is treated as a request for a static file, which static tries to reply.

What I would expect is that auth/index.js would be called allowed , but that does not happen .

In the main.js , I set the correct path as POST.

Within auth/index.js , I also use a POST.

The route I hope to follow is

  • POST /auth - > auth/index.js
  • POST /auth/allowed - > auth/allowed.js .

Well, I think that's it.

  • Where is my mistake?
  • How do I solve it?
  • asked by 17.04.2017 в 17:00

    1 answer



    After a few tests, in the end I've right with the answer.

    The issue is:

    1. In the router main , the routes to be mounted via router s may not have a bar at the end or > wildcards :

    /ruta/ - > Incorrect.
    /ruta/* - > Incorrect.
    /ruta - > Right !!

    2. In the main router, the routes to be mounted via additional router s must be mounted using use( )

    get( 'ruta', MIDDLEWARE ) - > Incorrect.
    use( 'ruta', MIDDLEWARE ) - > Right!

    3. The secondary router is used normally.

    Supports any method: get( ) , post( ) , ...

    Add a use( function( req, res, next ) { next( ); } ); to the end of the secondary router. No I am sure that this is mandatory, but in my specific case, with a final route (in the router main ) to manage the files static, it works correctly.

    Any breach of the above DOES NOT generate any error or warning or anything at all. Simply, our nested path is ignored . It can cost us a good time to discover what is happening: - (

    PD .-

    Many thanks to @GustavoGarcia for your interest and advice; -)

    answered by 17.04.2017 / 19:01