Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom response data after POST request. #129

Open
bjornba opened this issue Jun 23, 2016 · 1 comment
Open

Custom response data after POST request. #129

bjornba opened this issue Jun 23, 2016 · 1 comment
Labels

Comments

@bjornba
Copy link

bjornba commented Jun 23, 2016

Hello.

I'm using this package to store images uploaded with the Froala editor. When Froala sends a post request it expects to get a link to the uploaded image in response.

I managed to get this to work for me by creating a custom handler for post requests to my collection but what i'm wondering is whether it is possible to just change the response sent from the default post handler in the package.

My handler looks like:

handler: function (req, res, next) {
  // At the end of the handler function we call next() which calls file-collection's post handler.
  // In the post handler the file is saved to the gridFS collection and then the response headers
  // are written and the response ended.
  // To prevent file-collection's post handler from ending the response, we store the relevant functions
  // and replace them with our own where we can end the response with custom response headers/data.
  const storeResWriteHead = res.writeHead;
  const storeResEnd = res.end;

  // When file-collection's post handler calls writeHead/end, the functions below are called.
  // The stored functions are called with 'res' set as the 'this' object.
  res.writeHead = function() {
    storeResWriteHead.call(res, 200, {'Content-Type': 'application/json'});
  };
  res.end = function(){
    storeResEnd.call(res, '{"link": "gridfs/images/' + req.gridFS._id + '",'
      + '"fileName": "' + req.gridFS.filename + '",'
      + '"fileId": "' + req.gridFS._id + '" }');
  }

  // Let file-collection's post handler save the file.
  next();
}
@vsivsi
Copy link
Owner

vsivsi commented Jun 23, 2016

Hi, thanks for your question. I think that your solution is reasonable (the custom handler.) I would be hesitant to include the link in the default response to the POST request for a couple of reasons:

  • Having a non-empty default response would prevent another developer with a different use case from implementing their own request handler using next() to handle the actual POST handling.
  • Upload paths and download paths may not be the same, and for some uses the system may not even provide a mechanism to download the file (to the uploading user, or perhaps anyone). In this case, it may be impossible or undesirable to provide a download link.

Does this make sense to you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants