Home > Software engineering >  Get filename from route
Get filename from route

Time:07-19

I have route like /gcp/filename

import downloadGCP from './controllers/downloadGCP';
router.route('/gcp')
    .get(downloadGCP);

inside ./controllers/downloadGCP.js, how should I get the filename? Below is code for downloadGCP.js

export default async (req, res) => {
//<--To get filename
}

CodePudding user response:

The route /gcp will not match GET /gcp/filename request. I think the route should be /gcp/:filename (filename is a "variable" name).

Now, you can get filename value by req.params.filename.

CodePudding user response:

To get filename you need to first configure your router like this

import downloadGCP from './controllers/downloadGCP';
router.route('/gcp/:filename').get(downloadGCP);

and inside downloadGCP to get the file you need to get the file from request, like so:

export default async (req, res) => {
  const file = req.params.file 

  ...other stuff...
}

or depending on how you have structured and send your request

 export default async (req, res) => {
      const file = req.body.file // or req.file
    
      ...other stuff...
    }
  • Related