A
A
Al2016-09-06 12:57:21
Node.js
Al, 2016-09-06 12:57:21

How to upload files from a directory that is not accessible from outside?

Let 's say app/www/ - this is the folder where all the statics are located and it is visible to everyone from the browser, app/protected/ - here are the files that we will download.
How is it very easy and simple to do? - using Nginx and X-Accel-Redirect
But, the task is to do it without nginx, purely using the node.
The bottom line is, there is a url /download/16947 that we knock on (we only have an AJAX request available due to some features of the application), in the controller we can get information about the current file (its location in app/protected/ , etc.), and now we can give it to the client. But how to do it so that everything is very cool?
So far, I've come up with this: take this file, copy it toapp/www/tmp/ , we sculpt a link to it when it is already in tmp, and as a result, we direct the user already in a new tab, for example, to this temporary file. Then tmp will sometime clean up the garbage collector in the form of some kind of cronjob. It's bad and I don't like it. Do you have better ideas?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Wolf, 2016-09-06
@Sanu0074

router.get('/download/16947', function(req, res) {
  var pathToFile = getPathFile();
  fs.createReadStream(pathToFile).pipe(res);
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question