I have the following code. It lets me fetch an image, and specify optionally if a recache is needed. For example, /something.jpg/force
. It could also be the ID of an image, for example /7GDF4G
. As you can see, the force param is optional.
app.get('/:image/:force?*', function (req, res) {
console.log("image req");
// Create variable to hold force bool, default is false
var force = false;
// Check if force bool is present
if (req.params.force === ('force' || 'true' || 'recache' || 're-cache')) {
force = true;
}
// Pass req, res, image variable and force bool to process
mod.request.process({
req : req,
res : res
}, {
type : 'image',
id : req.params.image
}, force);
});
When run, everything is passed onto mod.request.process()
as expected. However, if I add the following to see what value is being returned for :image
:
console.log(JSON.stringify(data));
This is what is logged:
{"type":"image","id":"k"}
The input URL was kMoI9Vn.jpg/force
, so id
should be kMoI9Vn.jpg
. If I run it again with something.jpg
, I get {"type":"image","id":"s"}
.
Any ideas on why this might be happening?
Why not just remove the asterisk and use just /:image/:force?
?
This works for 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' ]