Express: Error router.param()

Created on 17 Apr 2014  路  11Comments  路  Source: expressjs/express

Unable to run your example :

router.param('id', /^\d+$/);

router.get('/user/:id', function(req, res){
  res.send('user ' + req.params.id);
});

(http://expressjs.com/4x/api.html#router)

Most helpful comment

+1 it does only accept a function

A simple modification could allow this (https://github.com/visionmedia/express/blob/master/lib/router/index.js#L93):

  if(fn instanceof RegExp) {
    fn = function() { 
      return param.match(fn); 
    }
  }

Is it intended behavior? (in the docs it provides Regexp atm)

I can't find how this was handle in express3.x, if someone has a clue please share :)

@DamienP33: take a look at https://github.com/visionmedia/express-params (not sure if it's compatible)

All 11 comments

what error are you getting?

Here is the error generated by the above example :

/home/damienp/workspace/RnD/demo_express/node_modules/express/lib/router/index.js:97
    throw new Error('invalid param() call for ' + name + ', got ' + fn);
          ^
Error: invalid param() call for id, got /^\d+$/
    at Function.proto.param (/home/damienp/workspace/RnD/demo_express/node_modules/express/lib/router/index.js:97:11)
    at Object.<anonymous> (/home/damienp/workspace/RnD/demo_express/routes/users.js:9:8)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/home/damienp/workspace/RnD/demo_express/app.js:10:14)
    at Module._compile (module.js:456:26)

the regular expression does not accept as a parameter.

+1 it does only accept a function

A simple modification could allow this (https://github.com/visionmedia/express/blob/master/lib/router/index.js#L93):

  if(fn instanceof RegExp) {
    fn = function() { 
      return param.match(fn); 
    }
  }

Is it intended behavior? (in the docs it provides Regexp atm)

I can't find how this was handle in express3.x, if someone has a clue please share :)

@DamienP33: take a look at https://github.com/visionmedia/express-params (not sure if it's compatible)

I think we just need to remove this from the docs @jonathanong @dougwilson? IMO if you want to perform validation that should just happen in the function you pass.

+1

was support for that removed? or was that never really supported?

No idea. I can't seem to find it in the 3.x branch (at least not from an initial glance, haven't tried running the example code).

ohhh i thought this was a bug. i guess it's a doc thing.

@visionmedia do you know?

Looks like a doc issue, it was not support in 2.x for sure (https://github.com/visionmedia/express/blob/3.5.2/lib/router/index.js#L67).

If this is not a bug, it is necessary to update the documentation

Actually I don't think it is a bug or a documentation issue. If you read the docs they say that in order to use this regex thing you need to have first specified a function to router.param which alters the behavior of the params API and allows you to then use this regexp stuff.

I think we should have removed this behavior in 4.x but whatever, it is there and it is properly documented as far as I can tell. If you find this not to be the case after reading the documentation, then please provide a full failing example that I can just node app.js and cause failure. As it stands, your example is incomplete based on the docs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

prashantLio picture prashantLio  路  3Comments

nove1398 picture nove1398  路  3Comments

despairblue picture despairblue  路  3Comments

afanasy picture afanasy  路  3Comments

Domiii picture Domiii  路  3Comments