如何阻止NodeJS的FS模块进行缓存?

huangapple go评论65阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年7月23日 17:01:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747410.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定