So I know that the fs module uses process.cwd() and not the actual location of the module but I'm still having a little issue with file paths.
So I've a app structure like this:
- app
- controllers
- models
- views
- tmp
- index.js
I'm creating csv files in a controller, and hope to store it in tmp.
So considering that despite being in controllers,
process.cwd() is '/app'
I did this:
fs.open('tmp/FILENAME','w',function (err,fd) {
err is null so that seems to work fine. So that should be in /app/tmp/
On the other hand, in index.js I have
res.attachment(__dirname + '/tmp/FILENAME')
which when console.logged is
/app/tmp/FILENAME
which is correct. So clearly the other half is causing the problem.
So my question is - What is the correct way to use fs in this scenario, taking into consideration process.cwd() ? Kinda stumped, any ideas?
relying on process.cwd() is risky, some other piece of code you introduce later on could change it.
using __dirname + '/tmp/FILENAME' is safer.