NodeJS how to read from /dev/pts/1 - javascript

Is it possible to read from /dev/pts/1 with nodeJS?
If i cat /dev/pts/1 i can see data.
I tryed to use this :
var fs = require('fs');
fs.readFile('/dev/pts/1', function(err, data) {
if (err) throw err;
console.log(data);
});
This give no output.
And also no error.
Image of running the script with no output

Related

How to retrieve a file using node.js fs readFile function without specifying the name?

I'm currently stuck trying to retrieve a file from file system in order to send it through api to the client. For my backend I'm using express js
I'm using fs library and currently I'm trying to do it with readFile function, but I want to do it without specifying the file name or just the file extension because it will depend from file file will be uploaded from client.
What I tried until now (unsuccessfully) is shown below:
router.get("/info/pic", async (req, res) => {
const file = await fs.readFile("./images/profile/me.*", (err, data) => {
if (err) {
console.log(err); // Error: ENOENT: no such file or directory, open './images/profile/me.*'
return;
}
console.log(data);
});
});
const file = await fs.readFile("./images/profile/*.*", (err, data) => {
if (err) {
console.log(err); // Error: ENOENT: no such file or directory, open './images/profile/*.*'
return;
}
console.log(data);
});
const file = await fs.readFile("./images/profile/*", (err, data) => {
if (err) {
console.log(err); // Error: ENOENT: no such file or directory, open './images/profile/*'
return;
}
console.log(data);
});
If I specify the file name everything works fine, like: fs.readFile("./images/profile/me.jpg". but as I said, I don't know for sure the right extension of that file.
Important info: In that directory there will be only one file!
Please help me!
Thank you in advance!
If there is only one file in the directory, the following loop will have only one iteration:
for await (const file of fs.opendirSync("./images/profile")) {
var image = fs.readFileSync("./images/profile/" + file.name);
...
}
const fs = require('fs');
fs.readdir('./images/profile', function (err, files) {
//handling error
if (err) {
return console.log('err);
}
files.forEach(function (file) {
// Do whatever you want to do with the file
});
});

ENOENT error on xml2js but file did exists

const xml2js = require('xml2js');
const fs = require('fs');
fs.readFile('https://www.tcmb.gov.tr/kurlar/today.xml', (err, data) => {
if(err) console.log(err);
var data = data.toString().replace("\ufeff", "");
xml2js.parseStringPromise(data, (err, res) => {
if(err){
console.log(err);
} else {
console.log(res);
}
});
});
This is my code in nodejs I try to get data on a https link with xml2js first by using the way it says in the npm page pf xml2js it gives some error and when I chechk on web I find solution of using with fs but still geting this error
I know the directory exists because if you go to link used in code it shows something but in code just gives error if someone can help I will be very happy
fs can only access file in your system, you should request the URL using http/https or even better try axios
const axios = require('axios')
axios.get('https://www.tcmb.gov.tr/kurlar/today.xml').then((response) => {
const data = response.data
xml2js.parseString(data, (err, res) => {
if(err){
console.log(err);
} else {
console.log(res);
}
})
})

fs Error: EISDIR: illegal operation on a directory, read

I'm getting "Error: EISDIR: illegal operation on a directory, read" after trying to read the content of a .json. This is how I'm trying to access the file. I'm using the FileSystem of node js.
fs.readFile( path, ( err, fileData) => {
if (err) {
throw err;
}
else {
return fileData;
}
});
While debugging I can see that the error is thrown before the if statement.
Any idea?
Maybe the path to the file is not the right one, make sure the path of your file looks like the one that appears in the following code:
const fs = require('fs');
fs.readFile('PATH_TO_YOUR_FILE/File_Name.json', (err, fileData) => {
if (err) {
throw err;
} else {
console.log(JSON.parse(fileData));
}
});

Write file from server returns success, but can not find file

I am trying to do a simple fs.writeFile on this server in meteor. It returns a success message for the majority of solutions I have tried. But I cannot find the file anywhere in the file structure. Ideas?
Here is a simple snippet I have tried.
const fs = require('fs');
var path = process.env['METEOR_SHELL_DIR'] + '/../../../public';
fs.writeFile('helloworld.txt', 'Hello World!', function (err) {
if (err) {
console.log("Error:" + err);
} else {
console.log("Success");
}
});
You're writing to 'helloworld.txt', but I think you meant to write to path+'/helloworld.txt'.

Error while inserting file to MongoDB from Node.js

I am trying to connect to mongoDB from node.js and upload a file("functions") to MongoDB.
Can someone please verify whats the issue with my code is.
When I run the js file, I am getting following error:
Error: Cannot find module 'mongodb'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
Code is as follows:
var mongodb = require('mongodb');
var url = require('url');
var log = console.log;
var currentTimeStamp = new Date();
var file = require (__dirname + '/functions');
mongodb.MongoClient.connect('mongodb://phx8b03c-fb1d-6.stratus.phx.ebay.com,phx8b03c-316d-6.stratus.phx.ebay.com,phx8b03c-9564-6.stratus.phx.ebay.com',
function (err, client) {
if (err) throw err;
client.createCollection('lbTopology' , function (err, collection) {
if (err) throw err;
collection.insert(file, 'lbTopology' , function (err) {
if (err) throw err;
client.close(function (err) {
if (err) throw err;
console.log('done');
});
});
});
});
Can someone please let me know what the issue is? Thanks a lot in advance
It looks like you don't have mongodb installed. Did you npm install mongodb in the same directory with your code or do you have a node_modules folder with mongodb in it?

Categories

Resources