Serving temporary files with NodeJs

I am creating a SOAP NodeJs client. Initially, I assumed that the server (i.e. the SOAP node client) will allow documents to be uploaded via the REST API (the REST API is authenticated). After a lot of time on Google and SO, it seems like this is not possible.

This means that when a document is requested to load, I will have to make a SOAP call to the document and return the URL to the REST client via AJAX.

For this I need:

  • Temporarily create a file in Node
  • get your url and go back to web client
  • When a file is requested and a response is sent, delete the file (for security purposes)

Here are my questions:

  • Is there already such an infrastructure? temp module may be an option, but actually I would like to remove it after each request, and not after a period of time.
  • If not, can I only do this using the NodeJs file system and Express static module ? Basically, we have to change the static module to look like this:

    return function static(req, res, next) { if ('GET' != req.method && 'HEAD' != req.method) return next(); var path = parse(req).pathname; var pause = utils.pause(req); /* My Added Code Here */ res.on('end', function(){ // delete file based on req URL }) /* end additions */ function resume() { next(); pause.resume(); } function directory() { if (!redirect) return resume(); var pathname = url.parse(req.originalUrl).pathname; res.statusCode = 301; res.setHeader('Location', pathname + '/'); res.end('Redirecting to ' + utils.escape(pathname) + '/'); } function error(err) { if (404 == err.status) return resume(); next(err); } send(req, path) .maxage(options.maxAge || 0) .root(root) .hidden(options.hidden) .on('error', error) .on('directory', directory) .pipe(res); }; 

Is res.on('end',... vaild? Alternatively, should I create some middleware that does this for URLs pointing to temporary files?

+4
source share
2 answers

Found two questions that answered my question. Therefore, obviously, we do not need to use express.static middleware. We just need the file system to download the file :

 app.get('/download', function(req, res){ var file = __dirname + '/upload-folder/dramaticpenguin.MOV'; res.download(file); // Set disposition and send it. }); 

If we want to stream and then delete , follow these steps:

 app.get('/download', function(req, res){ var stream = fs.createReadStream('<filepath>/example.pdf', {bufferSize: 64 * 1024}) stream.pipe(res); var had_error = false; stream.on('error', function(err){ had_error = true; }); stream.on('close', function(){ if (!had_error) fs.unlink('<filepath>/example.pdf'); }); 
+18
source

If you visit this SO page after December 2015, you may find that the previous solution does not work (at least it does not work for me). I found another solution, so I decided to provide it here for future readers.

 app.get('/download', function(req, res){ res.download(pathToFile, 'fileNameForEndUser.pdf', function(err) { if (!err) { fs.unlink(path); } }); }); 
+10
source

All Articles