arcade16
arcade16

Reputation: 1535

Pass parameter to node js API call?

I am generating preauthorized links for files from an S3 bucket, but would like to pass in the file name to download as a parameter.

This is what my API looks like:

reports.get('/xxx', async (req, res) => {

  var AWS = require('aws-sdk');

  var s3 = new AWS.S3();

  var params = { 
    Bucket: config.xxx,
    Key: 'xxx/xxx.json',
    Expires: 60 * 5
  }

  try {
    s3.getSignedUrl('getObject', params, function (err, url) {
      if(err)throw err;
      console.log(url)
      res.json(url);
    });
  }catch (err) {
    res.status(500).send(err.toString());
  }
});

And this is how I call it from the front end:

getPreauthorizedLink(e){
    fetch(config.api.urlFor('xxx'))
        .then((response) => response.json())
        .then((url) => {

        console.log(url);
    });
}

How can I add a parameter to the API call and corresponding API method to pass the filename?

Upvotes: 2

Views: 3126

Answers (1)

Muhammad Usman
Muhammad Usman

Reputation: 10148

Looks like you are using express on your server side so you can simply add parameters in the request URL and then get them on the server side.

In frontend or on your client side you will call the Api like

fetch('/xxx/FileName')

And on the backend you will modify your route like

 reports.get('/xxx/:fileName', ..){
   var fileName = req.params.fileName
}

Also you don't want to require everytime you receive a request. So you better move var AWS = require('aws-sdk'); outside your request handler.

Upvotes: 2

Related Questions