How do I read a file deeper within the file structure?
I have the following file structure on my server:
[Project]
[Routes]
testScripts.js
[Imports]
data-import.csv
server.js
data-project.csv
I am trying to open a CSV file using Node.js's FS object, and I'm having
trouble figuring out the correct path parameter. So far, I've been able to
use the following code to parse the CSV in the base folder (where
Server.js is running) but not the CSV in the Imports folder.
This code is returned by testScripts.js, but it is called by server.js.
var fs = require('fs');
exports.CSVload = function(req, res)
{
// body...
var fileName = './Imports/data-import.csv';
//reads the .csv file into a string
fs.readFile(fileName, 'utf8', function(error, data)
{
console.log('/Imports/data-import.csv, logs as undefined');
console.log(data);
});
fs.readFile('data-project.csv', 'utf8', function(error, data)
{
console.log('next to server.js, prints the contents of the CSV');
console.log(data);
});
};
What is the proper way to traverse the filetree for the FS object? Is
there some difference between declaring the filepath as a variable vs.
passing the string in directly? Is there some other difference I'm
missing?
No comments:
Post a Comment