1

I'm starting devel on very small app, and decided to use koa Framework with koa-router ( among some other middleware in the future)

Question is that after specifying my routes, a strange catch-all, no method, no url route appears

// excerpt of /app/routes/admin

var router = require('koa-router')().prefix('/admin');

router.get('/a.html', function *(next){
  return this.render('a', {some: 'data'})
})

router.get('/index.html', function *(next){
  return this.render('index', {})
})

router.get('/b.html', function *(next){
  return this.render('b', {})
})

module.exports = router

// excerpt of Main App
    
    // routes
    var adminRouter =  require('/app/routes/admin')
    this.app.use( adminRouter.routes())
    this.app.use( adminRouter.allowedMethods())
    
    // Checking Routes
    console.log(adminRouter.stack.map(i => i.path));

... which, when run, gives the following result:

    [ '(.*)',
  '/admin/a.html',
  '/admin/index.html',
  '/admin/b.html' ]

From where the hell is (.*) coming?

Also, if I enable the debugging for koa-router, I get

  koa-router defined route HEAD,GET /admin/list.html +46ms
  koa-router defined route HEAD,GET /admin/index.html +1ms
  koa-router defined route HEAD,GET /admin/queries.html +0ms
  koa-router defined route  (.*) +1ms

, so to make things even stranger, shows that this route has no associated method of any kind

Any idea of that "Phantom" entry?

mtsdev
  • 613
  • 5
  • 11
  • 1
    I just tried out your code with a [minimal example](https://gist.github.com/anonymous/26766ac4f6ba4125a5689e8e475f262d), and it seems to work fine so it must be coming from somewhere else that is not in the code you included. Could you share the rest of your code? – Saad Sep 14 '16 at 00:17
  • I just tried out you mini setup and it works for me too. Rest of code is like a 17 source files, but most of them have nothing to do with the http server, ON the rest of mmiddleware it's pretty basic, and I even commented out to do the testing , so basically what you've seen its what there is. – mtsdev Sep 14 '16 at 17:19
  • Anyway getting deepre in question, it seems that the phantom route just appers in the very same moment of importing adminRouter Routes into main router. I ve test addming some dummy routes on main router before that and Phantom route doesn't appear – mtsdev Sep 14 '16 at 18:16
  • Hmm, could you share more of your admin code? Right now, there isn't really a way to figure out what the issue is with just the code you currently have provided. – Saad Sep 15 '16 at 04:59
  • Ok, [here it is](https://gist.github.com/develmts/585ca1ed5c3c246cc4087e050d07a8da) – mtsdev Sep 15 '16 at 18:42
  • Dirty workaround follows – mtsdev Sep 15 '16 at 18:45
  • if (mainRouter.stack[0] == '(.*)') mainRouter.stack.shift() – mtsdev Sep 15 '16 at 18:46

1 Answers1

0

OK. Not exactly a solution, but at least it is not Phantom anymore After digging with the debugger, It seems that route aperas when a a middleware is added "globally" to the router, ie when your "use" it

router.use(anotherRouter.routes()

which makes perfect sense, as if you're adding a middleware "globally" that way, you expect that every request past through it

I opened the question because i was getting 404 not found For ALL my routes, and suspected this one was the offender, but in the end it was for a couple of different questions ( non reachable paths, non existent files, misconfiguration,etc)

mtsdev
  • 613
  • 5
  • 11