var express = require('express');
var app = express();
app.get('/files/:service/:op/:file', function(req, res){
console.log(req.params.file)
})
app.listen(3000);
Calling http://localhost:3000/files/farm/get/chicken will log chicken.
Calling http://localhost:3000/files/farm/get/barn/yard/chicken will log barn.
How can I make an Express app.VERB(path, callback) path log barn/yard/chicken?
Change your route to something like this:
app.get('/files/:service/:op/*', function(req, res){
console.dir(req.params);
console.log(req.params[0]); // this is what you want
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('OK');
});
Notice how the last parameter was changed to an * so that it takes single values (like chicken) or multi-values (like barn/yard/chicken). The issue here is that the slash character is used to split the URL components but you want to sometimes split the values but not others. The * will automatically lump the last set of values (1 or many) into a single element.
See http://expressjs.com/api.html#req.params
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With