0

A user comes to my site and inputs something, and my site generates a file as an output.

Unfortunately i cannot place the generated file on the public directory - as you all now Meteor watches this and restarts every time the public folder content is changed.

so my generated files lives in .meteor/local/build/programs/server/files

so for example i have document.pdf that lives in that directory, I'd like to serve/force/trigger a file download to my client's browser that lets his browser download this document.pdf file.

David
  • 4,235
  • 12
  • 44
  • 52

2 Answers2

1

In general Its not a very good idea to do this. It makes it very hard to scale your app. Node isn't good at serving chunky static files either.

Then also if you have two servers there is a slight chance that the other one's data is requested (e.g if you use a download manager).

I'm not sure but I think Meteor's live code reload doesn't work/is switched off in when in production mode (when using meteor deploy or meteor bundle)

The best thing to do would be to upload your file to S3 and then redirect the user to the file there.

Tarang
  • 75,157
  • 39
  • 215
  • 276
  • why would it be hard to scale the app? also am hoping not to use s3 or any other 3rd party service, i just wanna host my own files/everything. Right now my solution is putting it on a different root folder so nginx can just serve them and not meteor. – David Dec 05 '13 at 22:03
  • @david it becomes a problem when you have more than one server. The other server won't have the files. So your load balancer could request the data from the other server which wouldn't be the one storing them – Tarang Dec 06 '13 at 05:34
  • ahh that completely makes sense. +1 for you. – David Dec 06 '13 at 08:03
1

You can also use Iron Router and server side routes to create a dynamic file download. See Iron Router Server Side docs. Then you set your content type to application/pdf and send back the file directly without saving it to the filesystem. If you need to you can also save it in some other folder and serve it up yourself.

Then have a peek at this answer for an example of reading in and streaming out a file: Node JS file downloads using a stream.

Since this is a server side route, using express and Iron Router, you shouldn't have to mess with any of the fibers related async issues.

Community
  • 1
  • 1
MrMowgli
  • 873
  • 7
  • 23