node-tmp | Temporary file and directory creator for node.js | Runtime Evironment library
kandi X-RAY | node-tmp Summary
kandi X-RAY | node-tmp Summary
This is a widely used library to create temporary files and directories in a node.js environment. Tmp offers both an asynchronous and a synchronous API. For all API calls, all the parameters are optional. There also exists a promisified version of the API, see tmp-promise. Tmp uses crypto for determining random file names, or, when using templates, a six letter random identifier. And just in case that you do not have that much entropy left on your system, Tmp will fall back to pseudo random numbers. You can set whether you want to remove the temporary file on process exit or not. If you do not want to store your temporary directories and files in the standard OS temporary directory, then you are free to override that as well.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Sanitize options
- Make a temporary path for the specified options .
- Utility function to create a file
- Generate a tmp directory
- Prepare call to remove clean objects from the cache .
- Create a directory .
- Removes file from file system path .
- Synchronously create a new file .
- Asserts whether the given name is a relative path .
- Synchronously acquires a temporary file .
node-tmp Key Features
node-tmp Examples and Code Snippets
Community Discussions
Trending Discussions on node-tmp
QUESTION
I want to add a functionality in my server of MEAN stack: 1) entering localhost:3000/#/files
in a browser will create a temporary text file, which can be open by localhost:3000/#/{FILE_ID}.txt
; 2) closing the browser will delete the temporary file.
To this end, I try to use node-tmp, and have written some code:
In front-end:
...ANSWER
Answered 2017-Feb-20 at 21:40but closing the browser tab does NOT delete this file.
Closing a browser window does not normally give you any sort of server side notification that the browser window was closed.
There are various ways that developers work-around this:
Server-side session with session timeout. Create a server-side session object with a timeout and associate any created files with a particular session. When the session timeout expires because nobody who owns that session has visited the site in some period of time, then remove the files who's named are stored in that session.
WebSocket connection to monitor when connected. Use a webSocket connection from client to server to allow your server to monitor when the web page is still connected. Since the webSocket will be disconnected when the browser page is closed, you can use that event on the server to trigger the clearing of the files associated with that session. Note, that just navigating from one page to another within your site will also trigger the closing and then reopening of a webSocket so you will have to also use a timeout here. If a webSocket is closed by a user and then not reopened in some short period of time, you can assume the user has closed that browser window or navigated to some other site.
Hourly cleanup process. Use some sort of recurring cleanup process that ages away temp files based on the date/time they were created. Here, you could just run some function on a
setInterval()
every hour that checks for temp files that were created more than X hours ago and removes them.
let the tmp file be placed in localhost:3000/#/{FILE_ID}.txt
It's unclear exactly what you want here. http://localhost:3000/#/{FILE_ID}.txt is not a location in your file system. It's a URL that uses a hash location which won't even be passed directly to the server. In order to use that URL, you would have to have some client code that parsed the {FILE_ID}.txt
and then did something with it (perhaps put it in an Ajax call).
Using Express, the way something like this would work is that you would have some sort of generic express request handler that would look at something in the URL and then would serve the appropriate temporary file. You can decide where you want to put the temporary files in your file system (that's entirely up to you). You just make sure that the code that puts the files there and the code that serves them up from a request uses the same location.
For example, one URL scheme you could use for the files is like this:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install node-tmp
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page