Reputation: 7093
I am trying to create a file and save to path
with below code its creating file into records directory but filename is coming as ./app/records/server2b22f465-f7c9-4131-b462-93bc19760ab1.txt
path is included in file name , what i am missing so i can only save filename without the path into records folder ?
main.js
var uuid = require('node-uuid');
var fs = require('fs');
var path = './app/records'
var userLogs = function (data) {
var filename = 'server' + uuid.v4() + '.txt';
var file = path + '/' + filename;
fs.writeFile(file,data,function () {
console.log(file);
});
console.log('userLogs', data);
};
module.exports = userLogs;
Upvotes: 0
Views: 1081
Reputation: 12029
I believe that the path you pass to You can use node's native path module to resolve a relative path to an absolute path. The method to do so is fs.writeFile
needs to be absolute.path.resolve()
.
So try this:
var uuid = require('node-uuid');
var fs = require('fs');
var path = require('path');
var filePath = path.resolve('./app/records');
var userLogs = function (data) {
var filename = 'server' + uuid.v4() + '.txt';
var file = filePath + '/' + filename;
fs.writeFile(file,data,function () {
console.log(file);
});
console.log('userLogs', data);
};
module.exports = userLogs;
Edit: fs.writeFile
does not need an absolute path
According to the Node fs docs:
https://nodejs.org/api/fs.html#fs_file_system
The relative path to a filename can be used. Remember, however, that this path will be relative to
process.cwd()
Upvotes: 1