javascript - Route param only fetching first letter -
i have following code. lets me fetch image, , specify optionally if recache needed. example, /something.jpg/force
. id of image, example /7gdf4g
. can see, force param optional.
app.get('/:image/:force?*', function (req, res) { console.log("image req"); // create variable hold force bool, default false var force = false; // check if force bool present if (req.params.force === ('force' || 'true' || 'recache' || 're-cache')) { force = true; } // pass req, res, image variable , force bool process mod.request.process({ req : req, res : res }, { type : 'image', id : req.params.image }, force); });
when run, passed onto mod.request.process()
expected. however, if add following see value being returned :image
:
console.log(json.stringify(data));
this logged:
{"type":"image","id":"k"}
the input url kmoi9vn.jpg/force
, id
should kmoi9vn.jpg
. if run again something.jpg
, {"type":"image","id":"s"}
.
any ideas on why might happening?
why not remove asterisk , use /:image/:force?
?
this works both examples:
var pathtoregexp = require('path-to-regexp'); var keys = []; var exp = pathtoregexp('/:image/:force?', keys); console.dir(exp.exec('/7gdf4g')); console.dir(exp.exec('/something.jpg/force')); // outputs: // [ '/something.jpg/force', // 'something.jpg', // 'force', // index: 0, // input: '/something.jpg/force' ] // [ '/7gdf4g', '7gdf4g', undefined, index: 0, input: '/7gdf4g' ]
Comments
Post a Comment