'fs.writeFile works on localhost but not production server
I have a basic web app setup that has a file structure that resembles this:
WEB APP
- data
-year.txt
- cron
-write.js
One parent directory with two folders each containing a single file. The write.js file generates a bunch of data on the backend, JSON.stringifies that data and writes the string to the year.txt file located in the other folder. This is what it looks like in the write.js file:
try {
fs.writeFile(__dirname + '/../data/year.txt', JSON.stringify(organizedData), () => {
console.log('SUCCESS');
res.status(200);
res.send(null);
});
} catch (e) {
res.status(500);
console.log(e);
res.send(null);
}
All of this works fine on my LOCAL server, the write.js file updates the year.txt with the correct data. I'm just struggling to figure out why this wont work on my production server, which is hosted on Google Cloud App Engine. The part that really confuses me is when I check the logs on this production server I get the "SUCCESS" message, which means the fs.writeFile function isn't throwing any errors. I check the year.txt file url path, and nothing is added. I can't seem to figure this out, does it maybe have something to do with the file path in the writeFile function?
Solution 1:[1]
It's maybe related to file system read-write policy in your server environment, you need to verify is your node application have enough permission for writing file in that specific directory
logging the actual error will give you and me more info. You also need to read some documentation on your to find out where exactly you are allowed to write to from your server process.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | hasanqqsp |
