英文:
how to prevent NodeJS FS mdule from caching?
问题
我正在使用NodeJS和EJS制作文件管理器,我有一个默认的/folders
路由,使用fs.readdirSync(folderPath, { withFileTypes: true });
读取特定位置的所有文件和文件夹,另一个路由/create-folder
使用fs.mkdirSync(folderPath, {recursive:true});
创建新文件夹。
问题是,虽然更改立即在实际操作系统的文件系统上反映出来 - 文件夹和文件成功创建 - 但在/folders
路由中使用的fs.readdirSync()
中不会显示在Web应用程序上,我需要重新启动应用程序才能看到它们,而且由于它是一个文件管理器,我需要立即看到我创建的文件和文件夹,这是缓存问题吗?如果是,如何禁用它?
英文:
I'm making a file manager using NodeJS and EJS, I have a default /folders
route which reads all files and folders in a specific location using fs.readdirSync(folderPath, { withFileTypes: true });
and another route /create-folder
makes me a new folder using fs.mkdirSync(folderPath, {recursive:true});
the thing is while the changes are reflected on the actual OS's file system instantly - the folder & files are created successfully - it doesn't show on the web app using the mentioned fs.readdirSync()
in the /folders
route, I need to restart the application to be able to see them, and since it's a file manager I need to see my created files & folders immediately, is it a caching issue, and if so how can I disable it?
答案1
得分: 1
在再次调试后,我注意到 fs 模块会立即读取数据,正如它应该的那样,逻辑错误在于根据之前获取的 Mongoose 查询来过滤这些文件夹,并且其结果中并没有新创建的文件夹名称 - 这是在创建时我将其插入文档中的 - 因此在重新获取用户创建的文件夹后,一切正常运行。
英文:
After debugging again I noticed that the fs module reads the data instantly as it is meant to be, and the logical error was in filtering those folders based on a Mongoose query fetched a step before and its result doesn't have the newly created folder name - which I insert it into the document when creating -, so after re-fetching the user's created folders again, it all worked.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论