2

Hi I'm new to node and I'm having a routing issue with Express. I'm trying to create a mini MVC framework to create test projects with and learn node/noSQL. Most of the code is based off the MVC example (https://github.com/visionmedia/express/tree/master/examples/mvc) in the Express repo. In addition to that I've added extendable controllers with help from here: How to create extendable controllers in ExpressJS

Code: https://github.com/monsterlane/node-runner

The problem is in https://github.com/monsterlane/node-runner/blob/master/app/boot/index.js lines 33-43.

if ( key == 'index' && name == 'main' ) {
    method = 'get';
    path = '/';
}
else if ( key == 'index' ) {
    method = 'get';
    path = '/' + name;
}
else {
    throw new Error( 'unrecognized route: ' + name + '.' + key );
}

What I'm trying to do in this block is assign the main controller to respond to localhost/ and every other controller to respond to localhost/controller/. If I change line 35 to /main (instead of /) then 404's will correctly fall through the boot and into the error handler in app/index.js:

// load controllers
require( './boot' )( app, { verbose: !module.parent } );

// assume "not found" in the error msgs is a 404
app.use( function( err, req, res, next ) {
    // treat as 404
    if ( ~err.message.indexOf( 'not found' ) ) return next( );

    // log it
    console.error( err.stack );

    // error page
    res.status( 500 ).render( '5xx' );
});

With the code as is, if I go to any invalid URL, localhost/deep, localhost/doop they all return the main module? For some reason it looks like binding to '/' makes any invalid URL use this route?

Any ideas on what I'm doing wrong? Thanks!

Community
  • 1
  • 1
Jonathan
  • 543
  • 9
  • 23

1 Answers1

1

Changing app.use to app.all fixed the problem. Does anyone know what the difference is?

Jonathan
  • 543
  • 9
  • 23