Search code examples
node.jsrestifyformidable

Cleaning temporary files after file upload with restify


I am building a node storage server with restify. I am processing uploads via restify.bodyParser, which under the hood uses formidable.

Formidable stores files in os.tmpDir() by default, and I need to change that to some other folder, and so I did via restify.bodyParser({uploadDir: '/path/to/new/tmp'}).

The problem is that those temporary files are kept in the tmp directory even after I finish processing the request.

I have seen this question (Handling Temporary Files after upload), that supposes to just delete the tmp file after processing it.

My question is, do you need to always manually remove the file? Even with the default os.tmpDir() directory? How does the default system tmp folder work? Does it flush itself sometimes?


Solution

  • Generally, for file uploads, your code moves the uploaded file from its temporary location to a more permanent location. If, after processing the upload, you are left with a temporary file, you are supposed to clean it up manually (using fs.unlink()).

    On UNIX-type operating systems, os.tmpDir() (which is usually /tmp) isn't cleaned up periodically by the system (although it is typically cleared during system boot).