0
module.exports.validateFolder = async () => {
    fs.readdir('C:\\Users\\Kiko Pindoy\\Desktop\\Island Gas\\files\\unprocessed', (error, files) => {
        if(error) {
            throw error
        } else {
            if(files.length < 1) {
                let logsPath = 'C:\\Users\\Kiko Pindoy\\Desktop\\Island Gas\\files\\logs\\error'
                let processDate = new Date().toISOString().slice(0, 10)
                let content = `No Files Found in C:\\Users\\Kiko Pindoy\\Desktop\\Island Gas\\files\\unprocessed\n`
                fs.appendFile(`${logsPath}\\${processDate}.txt`, content, error => {
                    if(error) {
                        throw error
                    }
                })
            } else {
                for(let i = 0; i < files.length; i++) {
                    this.validateSheets(`C:\\Users\\Kiko Pindoy\\Desktop\\Island Gas\\files\\unprocessed\\${files[i]}`, files[i])
                }
            }
        }
    })}

I am trying to access my local folder using fs.readdir() and passing a path. When I run it locally through postman, "localhost:4000/api/readExcelFile", the function is running properly. But when I deploy it to heroku and run it through postman, "https://word-word-number.herokuapp.com/api/readExcelFile", it throws an Error below:

[Error: ENOENT: no such file or directory, scandir 'C:\Users\Kiko Pindoy\Desktop\Island Gas\files\unprocessed'] {
errno: -2,
code: 'ENOENT',
syscall: 'scandir',
path: 'C:\\Users\\Kiko Pindoy\\Desktop\\Island Gas\\files\\unprocessed'}

Is there something that I am missing? Or how can I be able to resolve this?

Kiko
  • 31
  • 6
  • 1
    Are you trying to read client-side files like this? You can’t - node only has access to the server side file system. It only works locally because it’s all your machine – Joe Nov 06 '22 at 03:25
  • The expectation is, the user will store the excel file in his local folder in his local machine/laptop. Then after storing it, he will call the API using postman, which reads the excel file and process it. There is no UI/Front-end for this application. The problem I am encountering is when the application is hosted in heroku, the path to the folder where the file is stored is not being read. – Kiko Nov 06 '22 at 03:32
  • 1
    This will never work, for good reason. Imagine the security implications of web applications being able to read arbitrary files from users' machines! Your code runs on the _server_, not you user's machine. On Heroku, `C:\\` doesn't even exist (it doesn't use Windows). If you had client-side JavaScript, it still won't work: browsers are full of security features that prevent you from accessing users' hard drives. You'll have to let them manually add the file using a file upload input. (The only reason this appears to work in development is that your laptop is both the server and the client.) – ChrisGPT was on strike Nov 06 '22 at 11:31

0 Answers0