0

I created this function so that when updating my application, I don't need to restart the node server, but I don't update the file I think it could be the accumulation of information, but I'm not sure And I'm having trouble finding all my app's files/folders, isn't there a better way to do this?

*refreshFolders() {
        try {
          const folders = fs.readdirSync(path.join(__dirname, "..", ".."));
    
          for (const folder of folders) {
            fs.readdirSync(path.resolve(__dirname, "..", ".."), { withFileTypes: true })
              .filter((type) => {
                if (type.isFile()) {
                  fs.watchFile(path.resolve(__dirname, "..", "..", type.name), (curr, prev) => {
                    delete require.cache[path.resolve(__dirname, "..", "..", type.name)];
                    utils.logger("info", `${type.name} updating...`);
                  });
                }
    
                if (type.isDirectory()) {
                  fs.watch(path.resolve(__dirname, "..", "..", type.name), (event_name, file_name) => {
                    delete require.cache[path.resolve(__dirname, "..", "..", type.name, file_name)];
                    utils.logger("info", `${file_name} updating...`);
                  });
    
                  fs.readdirSync(path.resolve(__dirname, "..", "..", type.name), { withFileTypes: true })
                    .filter((secondType) => {
                      if (secondType.isFile()) {
                        fs.watchFile(path.resolve(__dirname, "..", "..", type.name, secondType.name), (curr, prev) => {
                          delete require.cache[path.resolve(__dirname, "..", "..", type.name, secondType.name)];
                          utils.logger("info", `${secondType.name} updating...`);
                        });
                      }
    
                      if (secondType.isDirectory()) {
                        fs.watch(path.resolve(__dirname, "..", "..", type.name, secondType.name), (second_event_name, second_file_name) => {
    
                          delete require.cache[path.resolve(__dirname, "..", "..", type.name, secondType.name, second_file_name)];
                          utils.logger("info", `${second_file_name} updating...`);
                        });
    
                        fs.readdirSync(path.resolve(__dirname, "..", "..", type.name, secondType.name), { withFileTypes: true })
                          .filter((threeType) => {
                            if (threeType.isDirectory()) {
                              fs.watch(path.resolve(__dirname, "..", "..", type.name, secondType.name, threeType.name), (three_event_name, three_file_name) => {
    
                                delete require.cache[path.resolve(__dirname, "..", "..", type.name, threeType.name, three_file_name)];
                                utils.logger("info", `${three_file_name} updating...`);
                              });
                            }
                            
                            if (threeType.isFile()) {
                              fs.watchFile(path.resolve(__dirname, "..", "..", type.name, secondType.name, threeType.name), (curr, prev) => {
                                delete require.cache[path.resolve(__dirname, "..", "..", type.name, secondType.name, threeType.name)];
                                utils.logger("info", `${threeType.name} updating...`);
                              })
                            }
                          })
                      }
                    })
                }
              })
          }
        } catch (error) {
          utils.logger("error", error.stack);
        }
      }*
  • 1
    Just restart the server. – Joe Oct 14 '22 at 02:25
  • What are you hoping to accomplish by deleting items from the cache? That does NOT delete compiled code from within nodejs. – jfriend00 Oct 14 '22 at 02:55
  • @jfriend00 No, the code is intact, it just resets the cache and the node reads the "new". If you want to see more on the subject, in this [question](https://stackoverflow.com/questions/2925940/how-can-i-edit-on-my-server-files-without-restarting-nodejs-when-i-want-to-see-t) – João Paulo moura Oct 14 '22 at 05:16
  • @Joe I don't want to pause my app to reset there, I want to add and edit my files with node in progress – João Paulo moura Oct 14 '22 at 05:19
  • Node would only read the new code if you `require()` it again and assign that new module reference to something new and then use it. The prior code and any state it might have is still there. This is NOT the same as restarting your server. I still don't understand what you're trying to accomplish. You will have to essentially `require()` and reassign ALL module references for anything of significance to happen. I still like Joe's first comment above. Oh, and this leaks memory like crazy so should never be used in production. – jfriend00 Oct 14 '22 at 05:25
  • @jfriend00 I read an article some time ago that said there is a way to respond to requests made before your app is terminated. can you tell me how is this possible? – João Paulo moura Oct 14 '22 at 05:41
  • I'm not sure exactly what "respond to requests made before your app is terminated" has to do with your question. Are you just asking about `process.on('exit', ...)` or `process.on('beforeExit', ...)` – jfriend00 Oct 14 '22 at 21:01
  • I already solved the problem. It's a method called Graceful Shutdown, you can find more about it [here](https://nairihar.medium.com/graceful-shutdown-in-nodejs-2f8f59d1c357) – João Paulo moura Oct 20 '22 at 04:30

0 Answers0