0

I am building a route to download files from our server. The problem is that the filepath contains forward slashes and then the filePath variable only contains the first folder.

NOTE: Don't worry - the code locks the filepath down to specific folders by checking the first folder to ensure the path begins inside a download directory.

I tried using different delimiters than the normal slash, like a +. The code does actually work if I do this but that's such a terrible hack. Is there any other way to do this?

In other words, this works:

http://localhost:5000/files/C/temp+uploads+upload_c64bc04e02

But this doesn't work:

http://localhost:5000/files/C/temp/uploads/upload_c64bc04e02

I really feel like I should be finding a way to get this to work.


router.get('/files/:bucketCode/:filePath', auth.check, async (req, res) => {

    let bucketCode= req.params.bucketCode;
    let filePath = req.params.filePath;

Nick
  • 466
  • 1
  • 6
  • 12
  • Possible duplicate of [Express.js routing: optional splat param?](https://stackoverflow.com/questions/10020099/express-js-routing-optional-splat-param) – esqew Oct 24 '19 at 15:16
  • The splat is exactly what I wanted. Unfortunately, it's not working for me. router.get('/files/:bucketCode/:filePath*', auth.check, async (req, res) => { let filePath= req.params.filePath; console.log('filePath:', filePath); When I visit: http://localhost:5000/files/C/temp/uploads/upload_1e24aa56 The server console reads: filePath: C:\dh3\temp – Nick Oct 24 '19 at 15:39
  • If my route definition is '/files/:bucketCode/*', how do I get the value of the path? I've been looking for this answer for 30 mins. I must be searching wrong. – Nick Oct 24 '19 at 15:57

1 Answers1

0

I'm posting this answer because finding out how to specify and reference wildcards in route definitions was more difficult than it needs to be.

This did it for me:

...

router.get('/files/*', auth.check, async (req, res) => {

    let file_path = req.originalUrl.replace(req.baseUrl, '');
    let file_name = file_path.substring((file_path.lastIndexOf('/') + 1));

...

It's worth noting this is not designed to work for all cases. I have limited use cases where, for example, I know I'm never requesting a file from root and that there will always be a file and at least one folder in the path.

Nick
  • 466
  • 1
  • 6
  • 12