0

I was using fs to write values from textarea into a file and had issues with the path until I found on stackoverflow that the path has to be a tmp folder. I did that and the terminal shows the function was successful but now I don't know where that folder is and how I can access.

app.post('/scheduleTweet', function (req, res) {
 fs.writeFile('/tmp/scheduleTweet', req.body.text, err => {
  if (err) {
   console.error(err);
  } else{
   console.log("success");
  }
 });
})

Can somebody help me please? I'm self taught so not really sure what to google even.

  • The temp folder path is relative to the path where your project is located. Check it in the directory containing the above file – Charchit Kapoor Jun 26 '22 at 10:46
  • @CharchitKapoor I thought so too but it isn't there. Could it be saved in the server? I'm using nodejs and the code is in server.js. – Sugared Maple Jun 26 '22 at 10:57
  • I think writeFile will not create the directory, if it doesn't exists – Charchit Kapoor Jun 26 '22 at 11:01
  • Are you running this natively on your machine (and if so what OS is that)? or in docker? or in a VM? are you saying that you can't find the file `/tmp/scheduleTweet` on your filesystem, or that `fs.writeFile()` doesn't write it, or something else? – root Jul 05 '22 at 01:06

1 Answers1

1

If the path starts with a /, then fs.writeFile writes to that path on the current drive, for example, to C:/tmp/schedule.txt. Otherwise, it writes relative to the current working directory of the node process. It's easier if you make it relative to the directory containing your Javascript code: fs.writeFile(__dirname + "/tmp/schedule.txt", ...).

Heiko Theißen
  • 12,807
  • 2
  • 7
  • 31