How to read a file on the Meteor's backend? - javascript

For some reason, I need to modify my mongodb with brute force.
the expected data is in a file, and I need to update the mongodb's value by the read-out file stream. with node.js' help, I generate codes like this,
const fs = require('fs');
fs.open('./f.csv', 'r', (err, fd) => {
if(!err) {
fs.readFile('./server/f.csv', 'utf8', (err,data)=>{console.log(data);});
}
});
But, now I have a difficulty to find the file. the execution throws an error:
{ Error: ENOENT: no such file or directory, open './f.csv' errno: -2, code: 'ENOENT', syscall: 'open', path: './f.csv' }
I have tried to locate the file in the Meteor's public folder or server folder, which is also Meteor's backend, but the efforts are in vain. So how to make the codes find the file on Meteor's backend?
Any suggestion is welcome.

Easiest solution is to put the file in /private and access it using the Assets module:
https://docs.meteor.com/api/assets.html
Example: If you put the file in /private/f.csv
const data = Assets.getText('f.csv');
console.log(data)
// ... Do something with that data

Related

Node.js 'fs' throws an ENOENT error after adding auto-generated Swagger server code

Preamble
To start off, I'm not a developer; I'm just an analyst / product owner with time on their hands. While my team's actual developers have been busy finishing off projects before year-end I've been attempting to put together a very basic API server in Node.js for something we will look at next year.
I used Swagger to build an API spec and then used the Swagger code generator to get a basic Node.js server. The full code is near the bottom of this question.
The Problem
I'm coming across an issue when writing out to a log file using the fs module. I know that the ENOENT error is usually down to just specifying a path incorrectly, but the behaviour doesn't occur when I comment out the Swagger portion of the automatically generated code. (I took the logging code directly out of another tool I built in Node.js, so I'm fairly confident in that portion at least...)
When executing npm start, a few debugging items write to the console:
"Node Server Starting......
Current Directory:/mnt/c/Users/USER/Repositories/PROJECT/api
Trying to log data now!
Mock mode: disabled
PostgreSQL Pool created successfully
Your server is listening on port 3100 (http://localhost:3100)
Swagger-ui is available on http://localhost:3100/docs"
but then fs throws an ENOENT error:
events.js:174
throw er; // Unhandled 'error' event
^
Error: ENOENT: no such file or directory, open '../logs/logEvents2021-12-24.log'
Emitted 'error' event at:
at lazyFs.open (internal/fs/streams.js:277:12)
at FSReqWrap.args [as oncomplete] (fs.js:140:20)
Investigating
Now normally, from what I understand, this would just mean I've got the paths wrong. However, the file has actually been created and the first line of the log file has been written just fine
My next thought was that I must've set the fs flags incorrectly, but it was set to 'a' for append:
var logsFile = fs.createWriteStream(__logdir+"/logEvents"+dateNow()+'.log',{flags: 'a'},(err) =>{
console.error('Could not write new Log File to location: %s \nWith error description: %s',__logdir, err);
});
Removing Swagger Code
Now here's the weird bit: if I remove the Swagger code, the log files write out just fine and I don't get the fs exception!
This is the specific Swagger code:
// swaggerRouter configuration
var options = {
routing: {
controllers: path.join(__dirname, './controllers')
},
};
var expressAppConfig = oas3Tools.expressAppConfig(path.join(__dirname, '/api/openapi.yaml'), options);
var app = expressAppConfig.getApp();
// Initialize the Swagger middleware
http.createServer(app).listen(serverPort, function () {
console.info('Your server is listening on port %d (http://localhost:%d)', serverPort, serverPort);
console.info('Swagger-ui is available on http://localhost:%d/docs', serverPort);
}).on('error',console.error);
When I comment out this code, the log file writes out just fine.
The only thing I can think that might be happening is that somehow Swagger is modifying (?) the app's working directory so that fs no longer finds the same file?
Full Code
'use strict';
var path = require('path');
var fs = require('fs');
var http = require('http');
var oas3Tools = require('oas3-tools');
var serverPort = 3100;
// I am specifically tried using path.join that I found when investigating this issue, and referencing the app path, but to no avail
const __logdir = path.join(__dirname,'./logs');
//These are date and time functions I use to add timestamps to the logs
function dateNow(){
var dateNow = new Date().toISOString().slice(0,10).toString();
return dateNow
}
function rightNow(){
var timeNow = new Date().toTimeString().slice(0,8).toString();
return "["+timeNow+"] "
};
console.info("Node Server Starting......");
console.info("Current Directory: " + __dirname)
// Here I create the WriteStreams
var logsFile = fs.createWriteStream(__logdir+"/logEvents"+dateNow()+'.log',{flags: 'a'},(err) =>{
console.error('Could not write new Log File to location: %s \nWith error description: %s',__logdir, err);
});
var errorsFile = fs.createWriteStream(__logdir+"/errorEvents"+dateNow()+'.log',{flags: 'a'},(err) =>{
console.error('Could not write new Error Log File to location: %s \nWith error description: %s',__logdir, err);
});
// And create an additional console to write data out:
const Console = require('console').Console;
var logOut = new Console(logsFile,errorsFile);
console.info("Trying to log data now!") // Debugging logging
logOut.log("========== Server Startup Initiated ==========");
logOut.log(rightNow() + "Server Directory: "+ __dirname);
logOut.log(rightNow() + "Logs directory: "+__logdir);
// Here is the Swagger portion that seems to create the behaviour.
// It is unedited from the Swagger Code-Gen tool
// swaggerRouter configuration
var options = {
routing: {
controllers: path.join(__dirname, './controllers')
},
};
var expressAppConfig = oas3Tools.expressAppConfig(path.join(__dirname, '/api/openapi.yaml'), options);
var app = expressAppConfig.getApp();
// Initialize the Swagger middleware
http.createServer(app).listen(serverPort, function () {
console.info('Your server is listening on port %d (http://localhost:%d)', serverPort, serverPort);
console.info('Swagger-ui is available on http://localhost:%d/docs', serverPort);
}).on('error',console.error);
In case it helps, this is the project's file structure . I am running this project within a WSL instance in VSCode on Windows, same as I have with other projects using fs.
Is anyone able to help me understand why fs can write the first log line but then break once the Swagger code gets going? Have I done something incredibly stupid?
Appreciate the help, thanks!
Edit: Tried to fix broken images.
Found the problem with some help from a friend. The issue boiled down to a lack of understanding of how the Swagger module works in the background, so this will likely be eye-rollingly obvious to most, but keeping this post around in case anyone else comes across this down the line.
So it seems that as part of the Swagger initialisation, any scripts within the utils folder will also be executed. I would not have picked up on this if it wasn't pointed out to me that in the middle of the console output there was a reference to some PostgreSQL code, even though I had taken all reference to it out of the main index.js file.
That's when I realised that the error wasn't actually being generated from the code posted above: it was being thrown from to that folder.
So I guess the answer is don't add stuff to the utils folder, but if you do, always add a bunch of console logging...

nodejs not creating the file that specified using fs.open and throwing error 4058-ENOENT

I am using this block of code to create and write a new directory and a file.
I just started to learn nodejs
var lib = {};
lib.baseDir = path.join(__dirname,'/../.data/');
lib.create = function(dir,file,data,callback){
fs.open(lib.baseDir+dir+'/'+file+'.json', 'wx', function(err, fileDescriptor){
if(!err && fileDescriptor){
var stringData = JSON.stringify(data);
// Write to file and close it
fs.writeFile(fileDescriptor, stringData,function(err){
if(!err){
fs.close(fileDescriptor,function(err){
if(!err){
callback(false);
} else {
callback('Error closing new file');
}
});
} else {
callback('Error writing to new file'+'lib.baseDir');
}
});
} else {
callback(err);
}
});
};
but I am repeatedly getting the error
{ Error: ENOENT: no such file or directory, open 'C:\Users\Jawahr\Documents\GitHub\node-api\.data\test\newFile.json'
errno: -4058,
code: 'ENOENT',
syscall: 'open',
path: 'C:\\Users\\Jawahr\\Documents\\GitHub\\node-
api\\.data\\test\\newFile.json' }
calling this library in a index.js as
var _data = require('./lib/data');
_data.create('test','newFile', {"asdksadlk" : "asldj"} ,function(err) {
console.log('this was the error ',err);
});
I've been stuck here for a while, is it because the pathname and filename contained the part "C:" which has colon a reserved char in windows 10, if it is the problem how to solve this.
using windows 10 and NodeJs 8.6.
Can you try this -
fs.open(lib.baseDir+dir+'/'+file+'.json', 'w', function(err, fileDescriptor){
It looks like 'wx' throws an error if file exists -
'w' - Open file for writing. The file is created (if it does not exist) or truncated (if it exists).
'wx' - Like 'w' but fails if the path exists.
'w+' - Open file for reading and writing. The file is created (if it does not exist) or truncated (if it exists).
'wx+' - Like 'w+' but fails if the path exists.
Referred from here
Looks like your put non-existing or non-accessible path to your file. Look:
fs.open('/path/is/not/exists/xx.js','wx',(err,fd)=>{
if (err) {
console.log(err.message);
}
});
and got
Error: ENOENT: no such file or directory, open '/path/is/not/exists/xx.js'
In case when file is already exists you will got something like Error: EEXIST: file already exists, open '...'
And last, but not least. Instead of lib.baseDir+dir+'/'+file+'.json' better solution is use path.join(lib.baseDir,dir,file+'.json') from path module
Add a check for directory or create before fs.open
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
}
Then the rest of your code will work. as fs.open only creates file if it doesn't exist, it does not create directory

Google Drive API : ENOENT - but my file is just in the same directory

ENOENT - but my file is just in the same directory ...
Hi all I try to sent a file, birds.mp3, to my Google Drive using API. The file got to be reached by a function to be sent.
Despite that the file I try to send is just in the same folder that the code concerned, my console return me :
events.js:183
throw er; // Unhandled 'error' event
^
Error: ENOENT: no such file or directory, open './birds.mp3'
Here my tree :
-- folder
|-- birds.mp3
|-- quickstart.js
Here my quickstart.js
module.exports.insertDrive = function (req) {
console.log("callback reached.")
var url = req.body.url;
var folderId = 'id';
var fileMetadata = {
'name': req.body.word,
parents: "id"
};
var media = {
mimeType: 'audio/*',
body: fs.createReadStream("./birds.mp3") // PATH FUNCTION HERE
};
drive.files.create({
resource: fileMetadata,
media: media,
fields: 'id'
}, function (err, file) {
if (err) {
// Handle error
console.error(err);
} else {
console.log('File Id: ', file.id);
}
})};
I can't figure out why my file can't be reached. I have try several tricks like path.resolve and all, I have try to push my birds.mp3 in several folder if any, but that have failed.
Thanks.
If you're trying to load a file from the same directory as the current module and pass that to the Google API, then use __dirname instead of ./. The ./ uses the current working directory which will depend upon how the overall program was invoked and will not point at your module directory.
So, if the intended file is in the same directory as your module, then change this:
fs.createReadStream("./birds.mp3")
to this:
fs.createReadStream(path.join(__dirname, "birds.mp3"));

Passing csv file as command like argument in nodeJs

I can't for the life of me think of a way to do this. I've previously worked with importing csv files into js files, but for this challenge I've got to create a js file that executable from a shell with the data file passed as input.
Any ideas how can this be done?
~The challenge description~
The program must be executable from a shell and take the CSV file as input. For example:
node score_calculator.js data.csv
You can take the file path as command line argument when running your nodejs app like
node myScript.js pathToCsvFile
Now you'll have to get this path in your code as
var filePath = process.argv[2];
Now you can use this path to read your file as
fs.readFile(filePath, (err, data) => {
if (err) throw err;
console.log(data);
});
Read more about file handling in nodejs here
Hope this helps
Sounds like you are trying to figure out how to pass and read parameters
You could do node score_calculator.js data.csv
then in your js
const csv = process.argv[2];
However. If you are passing in parameters I would recommend using minimist then you can do
node score_calculator.js --file=data.csv
And you js file would be
const argv = require('minimist')(process.argv.slice(2));
const csv = argv.file;

delete folder containing files node js

I am looking for a way to delete folders that contain files in node.js ?
I know there exists a way to delete empty folders using fs.rmdir(), and i tried using the npm rimraf module that provides the function "rm -rf" for node.js
socket.on("end", function (data) {
rimraf("./a/b/c", function(err){
if(err){
console.log(err);
}
});
});
but i keep getting this error.
{ [Error: ENOTEMPTY: directory not empty, rmdir './a/b/c']
errno: -39,
code: 'ENOTEMPTY',
syscall: 'rmdir',
path: './a/b/c' }
So I tried another way around this issue, first i empty the directory then i delete the directory
socket.on("end", function (data) {
rimraf("./a/b/c/*", function(err){
if(err){
console.log(err);
}else{
fs.rmdir("./a/b/c")
}
});
});
but then i get this error
Error: ENOTEMPTY: directory not empty, rmdir './a/b/c'
at Error (native)
I checked the folders the rimraf deletes the files but i don't see why i am still getting an error with fs.rmdir().
Edit :
I looked up a different module called fs-extra and came up with this.
fse.emptyDir("a/b/c/", function(err){
if(err){
console.log(err);
} else {
console.log("doneaaaa")
fse.remove("a/b/c",function(err){
if(err){
console.log(err);
} else {
console.log('doneaswell');
}
});
}
});
Now i get this error :
doneaaaa
{ [Error: EBUSY: resource busy or locked, unlink 'a/b/c/.nfs000000002ab5000d00000072']
errno: -16,
code: 'EBUSY',
syscall: 'unlink',
path: 'a/b/c/.nfs000000002ab5000d00000072' }
As you can see i get the past the first part of the function that deletes the files from the folder but when it comes to deleting the folder it throws the EBUSY error.
Thank you in advance !
Regardin the EBUSY error, do one thing. Do console.log(process.cwd()) to see what directory Node process is in before it tries to delete the folder. If Node is in the same folder it is trying to delete, then it will issue the EBUSY error. I had that happen to me in a Node.js application I'm developing. The solution was to change directory (process.chdir(new directory)) to something other than the one I'm trying to delete before I attempt to delete it, and problem solved. This happened on Windows by the way.
To remove it syncronously:
var fs = require('fs');
var deleteFolderRecursive = function(path) {
if( fs.existsSync(path) ) {
fs.readdirSync(path).forEach(function(file,index){
var curPath = path + "/" + file;
if(fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
};

Categories

Resources