I am trying to write a request from an API endpoint to a file with node packages express
and file-system
. However, when I run the writeFile
function, neither a folder or txt file is written to the file system.
I have checked the folder permissions and they seem to be set correctly. Also, the error callbacks seem to return null in the console log.
Server code
app.use((req, res, next) => { // Enable Cross-Origin Resource Sharing (CORS)
res.header("Access-Control-Allow-Origin", "*")
res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT")
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, content-type, Accept, Authorization, x-api-key")
next()
})
app.post("/node/api/mssql/post-order-header", (req, res, next) => {
if(valid) write.orderHeader(req, res)
})
Module code
let fs = require("fs")
...
exports.orderHeader = (req, res, next) => {
fs.writeFile('_log/receipts/test.txt', JSON.stringify(req.body), (err) => {
if (err) throw err;
console.log('The file has been saved!');
})
})
Error
C:\...\fs\write_order.js:17
if (err) throw err;
Error: ENOENT: no such file or directory, open 'C:...\test-folder\test.txt'
Is this possible to do with Node? (Want to create a new file each time)