I'm a beginner level programmer. I used some online guides as well as my starter knowledge. I'm attempting to create a bot that posts to twitter every hour. I keep getting the error ENOENT, which as I understand, means that it can't find the directory. Here is the error I'm getting (All I censored was personal information, but it's not censored in the actual code or error)
opening an image...
15.jpg
internal/fs/utils.js:269
throw err;
^
Error: ENOENT: no such file or directory, open 'C:\Users\####\Desktop\####\bot\images15.jpg'
at Object.openSync (fs.js:462:3)
at Object.readFileSync (fs.js:364:35)
at C:\Users\####\Desktop\####\bot\server.js:32:29
at FSReqCallback.oncomplete (fs.js:156:23) {
errno: -4058,
syscall: 'open',
code: 'ENOENT',
path: 'C:\\Users\\#####\\Desktop\\####\\bot\\images15.jpg'
}
It looks like its changing the name of the file to have images in front. I can't figure out why it's doing this though. Anyone know what I'm doing wrong? Here's my code:
const fs = require('fs'),
path = require('path'),
Twit = require('twit'),
config = require(path.join(__dirname, 'config.js')),
images = require(path.join(__dirname, 'images.js'));
const T = new Twit(config);
function randomFromArray(images) {
return images[Math.floor(Math.random() * images.length)];
}
function tweetRandomImage() {
fs.readdir(__dirname + '/images', function (err, files) {
if (err) {
console.log('error:', err);
}
else {
let images = [];
files.forEach(function (f) {
images.push(f);
});
console.log('opening an image...');
const image = randomFromArray(images);
console.log(image);
const imagePath = path.join(__dirname, '/images' + image);
const imageSource = image.source
b64content = fs.readFileSync(imagePath, { encoding: 'base64' });
console.log('uploading an image...');
T.post('media/upload', { media_data: b64content }, function (err, data, response) {
if (err) {
console.log('error:', err);
}
else {
console.log('image uploaded, now tweeting it...');
T.post('statuses/update', {
media_ids: new Array(data.media_id_string)
},
function (err, data, response) {
if (err) {
console.log('error:', err);
}
else {
console.log('posted an image!');
}
}
);
}
});
}
});
}
setInterval(function () {
tweetRandomImage();
}, 10000);
Any help would be appreciated, thank you!
lesson of the day, ALWAYS WATCH THE SynTAx
const fs = require('fs'),
path = require('path'),
Twit = require('twit'),
config = require(path.join(__dirname, 'config.js')),
images = require(path.join(__dirname, 'images.js'));
const T = new Twit(config);
function randomFromArray(images) {
return images[Math.floor(Math.random() * images.length)];
}
function tweetRandomImage() {
fs.readdir(__dirname + '/images', function (err, files) {
if (err) {
console.log('error:', err);
}
else {
let images = [];
files.forEach(function (f) {
images.push(f);
});
console.log('opening an image...');
const image = randomFromArray(images);
console.log(image);
//THE ONLY CHANGE I MADE BELOW
const imagePath = path.join(__dirname, '/images/' + image);
//THE ONLY CHANGE I MADE ABOVE
//THE ONLY PROBLEM WAS THAT YOU TRIED GETTING A FILE BUT HAVING A FOLDER AND A FILE NAME AS 1 THING UNSEPARATED BY "/" OR "\\" AND THE COMPUTER(& me who read it at first) THOUGHT TO LOOK FOR A FILE CALLED "images15.jpg" instead of "images/15.jpg"
const imageSource = image.source
b64content = fs.readFileSync(imagePath, { encoding: 'base64' });
console.log('uploading an image...');
T.post('media/upload', { media_data: b64content }, function (err, data, response) {
if (err) {
console.log('error:', err);
}
else {
console.log('image uploaded, now tweeting it...');
T.post('statuses/update', {
media_ids: new Array(data.media_id_string)
},
function (err, data, response) {
if (err) {
console.log('error:', err);
}
else {
console.log('posted an image!');
}
}
);
}
});
}
});
}
setInterval(function () {
tweetRandomImage();
}, 10000);
Related
I am trying to implement a feature to my web app where you can upload CSV files and insert data into Postgresql. I have made my app endpoint and written some code
const router = require('express').Router()
const uploadMid = require('./fileUpMid')
const pool = require('./db')
const fs = require("fs");
const fastcsv = require("fast-csv");
const upload = async (req, res) => {
if (req.files === null) {
return res.status(400).json({ msg: 'No file uploaded' });
}
const file = req.files.file;
file.mv(`${__dirname}/uploads/${file.name}`, err => {
if (err) {
console.error(err);
return res.status(500).send(err);
}
res.json({ fileName: file.name, filePath: `/uploads/${file.name}` });
});
let persons = [];
let path = __dirname + "/uploads/" +file.name;
fs.createReadStream(path)
.pipe(fastcsv.parse({ headers: true }))
.on("error", (error) => {
console.error(error.message);
})
.on("data", (row) => {
persons.push(row);
})
.on("end", () => {
//remove head
persons.shift();
const q = "some query here";
pool.connect((err, client, done) => {
if (err) throw err;
try {
persons.forEach(row => {
console.log(typeof row)
var obj = JSON.parse(JSON.stringify(row));
var values = Object.keys(obj).map(function (key) { return obj[key]; });
console.log(values)
client.query(q, values, (err, res) => {
if (err) {
console.log(err.stack);
} else {
console.log("inserted " + res.rowCount + " row:", row);
}
});
});
} finally {
done();
}
});
})
// fs.unlinkSync(path)
}
router.post('/file', uploadMid.single("file") ,upload)
module.exports = router
Everything seemed to work fine, but when I try to upload a second file I awlways get an error on terminal
Error: ENOENT: no such file or directory, open 'filename here with full path'
>- Emitted 'error' event on ReadStream instance at:
>- at internal/fs/streams.js:126:14
>- at FSReqCallback.oncomplete (fs.js:180:23) {
>- errno: -4058,
>- code: 'ENOENT',
>- syscall: 'open',
>- path: 'filename here with full path'}
I know this is not a safe nor secure way to upload data but this app is intended to be run only locally. Even when the first file is upload successfully in DevTools console it logs
GET http://localhost:3000/uploads/filename [HTTP/1.1 404 Not Found 8ms]
But the file is created with all its content on uploads directory.
Any tip for what to look for ?
Thank you in advance :)
Judging by the error (Error: ENOENT: no such file or directory, open 'filename here with full path'), here is the suggested way of defining paths in NodeJS apps using the path module.
const path = require('path');
// Inside`upload` middleware
const filePath = path.join(__dirname, 'uploads', file.name);
I'm trying to make a OCR parse of an image.
All the things works well but I have a problem this mongoose and syncronysm.
But cannot use "await" on the mongoose find call as the function is not async. How do I solve that.
Here is my code:
// post processImage
router.post('/', async (req, res) => {
try {
var baseUrl;
const form = formidable({ multiples: true });
form.parse(req, function (error, fields, files) {
var imatgeAProcessar = files.image.path;
var extname = path.extname(files.image.name);
getTextFromImage(imatgeAProcessar) // OCR process of the image
.then(res => {
const boss_name_req = res.boss_name;
const boss = Boses.findOne({"name" : boss_name_req}).exec();
// ERROR HERE // return nothing althought it exist on database (no await?)
console.log(JSON.stringify(boss)); // writes "{}"
const processedImage = {
"success": true,
"boss_name": boss.name,
"boss_image": baseUrl + 'images/' + boss.num + ".png"
}
res.json(processedImage);
})
});
} catch (err) {
res.json({message: err});
}
});
*edited
// post processImage
router.post('/', async(req, res) => {
try {
var baseUrl;
const form = formidable({ multiples: true });
var formfields = await new Promise(function(resolve, reject) {
form.parse(req, function(err, fields, files) {
if (err) {
reject(err);
return;
}
resolve(files);
}); // form.parse
});
var imatgeAProcessar = formfields.image.path;
var extname = path.extname(formfields.image.name);
const res = await getTextFromImage(imatgeAProcessar)
const boss_name_req = res.boss_name;
const boss = await Boses.findOne({ "name": boss_name_req }).limit(4).skip(0).exec();
const processedImage = {
"success": true,
"boss_name": boss.name,
"boss_image": baseUrl + 'images/' + boss.num + ".png"
}
res.json(processedImage)
} catch (err) {
res.json({ message: err });
}
});
Finally I found the way... I wrote a callback on the findOne call as :
const boss = Boss.findOne({"name" : boss_name_req})
.then( resMongoose => {
try {
const processedImage = {
"success": true,
"gym": resOCR.gym,
"boss_name": resMongoose.name,
}
res.json(processedImage);
} catch (err) {
res.json({message: err});
}
});
I am very new to Node js, I just want to get the data from extenal xml from a website but I got an error from Firebase Function log TypeError: invalid media type. I think it come from when I try to do this task parseString(xml, function(err, result) { })
Anyone can help me, it will be great:
Here is my code on firebase function:
exports.getRate = functions.https.onRequest((req, res) => {
getRate = () => {
var url = "https://www.vietcombank.com.vn/ExchangeRates/ExrateXML.aspx";
https.get(url, function(res) {
var xml = "";
res.on('error', function(error){
console.log(error, 'get data error');
})
res.on("data", function(chunk) {
xml += chunk;
console.log(xml, 'xml file');
});
res.on("end", function() {
var date = "";
let rateAUD = {
code: 'AUD/VND',
buy: 0,
sell: 0
};
let rateUSD = {
code: 'USD/VND',
buy: 0,
sell: 0
};
parseString(xml, function(err, result) {
console.log(xml, 'xml file');
date = result.ExrateList.DateTime[0];
if (result.ExrateList.Exrate[0].$.CurrencyCode == "AUD") {
rateAUD.buy = result.ExrateList.Exrate[0].$.Buy;
rateAUD.sell = result.ExrateList.Exrate[0].$.Sell;
} else {
console.log("They change the database list");
}
if (result.ExrateList.Exrate[18].$.CurrencyCode == "USD") {
rateUSD.buy = result.ExrateList.Exrate[18].$.Buy;
rateUSD.sell = result.ExrateList.Exrate[18].$.Sell;
} else {
console.log("They change the database list");
}
console.log(rateAUD, rateUSD, 'get data');
uploadDataToServer(date, { rateAUD, rateUSD });
if(err) {
console.log(err);
}
});
});
});
};
function uploadDataToServer(date, { rateAUD, rateUSD }) {
var db = admin.firestore();
let data = { rateAUD, rateUSD };
data.timeStamp = date;
console.log('upload success');
db.collection("liveRate").add(data),then((err)=> {
console.log(err);
});
}
return res.status(200)
.type('application / json')
.send('hello')
});
'
When I run the same code on another Nodejs playground, it works well.
Here is the link: https://repl.it/repls/MaroonSlateblueProfiler
So weird!
Ps: my payment option is ON.
The problem is that the client is sending the server what may or may not be a valid media type in an encoding the server cannot understand (as per the Content-Encoding header the client packaged with the request message).
Please try to set the content-type to xml:
getRate = () => {
var options = {
hostname: "www.vietcombank.com.vn",
port: 443,
path: "/ExchangeRates/ExrateXML.aspx",
headers: {
'Content-Type': 'application/xml'
}
};
https.get(options, function(res) {
...
});
}
I have download a zip file from s3 bucket then extracting the zip file
and finally upload one file to s3 bucket in Lambda function using
Node JS.But am getting the error
==> Error: EROFS: read-only file system, open './tmp/test.zip'
"Process exited before completing> request"
exports.handler = function (callback) {
downloadZipFile(params, downloadPath, function (err) {
if (err) {
callback(err);
} else {
processZipFile(downloadPath, function (err) {
if (err) {
callback(err);
} else {
callback(null);
}
});
}
});
};
function downloadZipFile(params, downloadPath, callback) {
const file = fs.createWriteStream(downloadPath);
s3.getObject(params)
.on('httpData', function (chunk) {
file.write(chunk);
})
.on('success', function () {
callback(null);
})
.on('error', function (err) {
callback(err);
})
.on('complete', function () {
file.end();
})
.send();
}
function processZipFile(filePath) {
const stats = fs.statSync(filePath)
const fileSizeInBytes = stats.size
if (fileSizeInBytes > 0) {
var srcPath = filePath;
var destPath = "./tmp";
targz.decompress({
src: srcPath,
dest: destPath
}, function (err) {
if (err) {
console.log(err);
} else {
console.log("Done!");
UploadFile();
}
});
}
}
function UploadFile() {
var body = fs.createReadStream('./tmp/SampleFile.txt')
var srcfileKey = "SampleFile.txt";
// Upload the stream
var s3obj = new AWS.S3({ params: { Bucket: bucketName, Key: srcfileKey } });
s3obj.upload({ Body: body }, function (err, data) {
if (err) {
console.log("An error occurred", err);
}
console.log("Uploaded the file at", data.Location);
})
}
You need to change the file path to just /tmp instead of ./tmp. Lambda only allows you to write to the /tmp directory.
I'm quite puzzled with reading files in Node.js.
fs.open('./start.html', 'r', function(err, fileToRead){
if (!err){
fs.readFile(fileToRead, {encoding: 'utf-8'}, function(err,data){
if (!err){
console.log('received data: ' + data);
response.writeHead(200, {'Content-Type': 'text/html'});
response.write(data);
response.end();
}else{
console.log(err);
}
});
}else{
console.log(err);
}
});
File start.html is in the same directory with file that tries to open and read it.
However, in the console I get:
{ [Error: ENOENT, open './start.html'] errno: 34, code: 'ENOENT', path: './start.html' }
Any ideas?
Use path.join(__dirname, '/start.html');
var fs = require('fs'),
path = require('path'),
filePath = path.join(__dirname, 'start.html');
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (!err) {
console.log('received data: ' + data);
response.writeHead(200, {'Content-Type': 'text/html'});
response.write(data);
response.end();
} else {
console.log(err);
}
});
Thanks to dc5.
With Node 0.12, it's possible to do this synchronously now:
var fs = require('fs');
var path = require('path');
// Buffer mydata
var BUFFER = bufferFile('../public/mydata.png');
function bufferFile(relPath) {
return fs.readFileSync(path.join(__dirname, relPath)); // zzzz....
}
fs is the file system. readFileSync() returns a Buffer, or string if you ask.
fs correctly assumes relative paths are a security issue. path is a work-around.
To load as a string, specify the encoding:
return fs.readFileSync(path,{ encoding: 'utf8' });
1).For ASync :
var fs = require('fs');
fs.readFile(process.cwd()+"\\text.txt", function(err,data)
{
if(err)
console.log(err)
else
console.log(data.toString());
});
2).For Sync :
var fs = require('fs');
var path = process.cwd();
var buffer = fs.readFileSync(path + "\\text.txt");
console.log(buffer.toString());
simple synchronous way with node:
let fs = require('fs')
let filename = "your-file.something"
let content = fs.readFileSync(process.cwd() + "/" + filename).toString()
console.log(content)
Run this code, it will fetch data from file and display in console
function fileread(filename)
{
var contents= fs.readFileSync(filename);
return contents;
}
var fs =require("fs"); // file system
var data= fileread("abc.txt");
//module.exports.say =say;
//data.say();
console.log(data.toString());
To read the html file from server using http module. This is one way to read file from server. If you want to get it on console just remove http module declaration.
var http = require('http');
var fs = require('fs');
var server = http.createServer(function(req, res) {
fs.readFile('HTMLPage1.html', function(err, data) {
if (!err) {
res.writeHead(200, {
'Content-Type': 'text/html'
});
res.write(data);
res.end();
} else {
console.log('error');
}
});
});
server.listen(8000, function(req, res) {
console.log('server listening to localhost 8000');
});
<html>
<body>
<h1>My Header</h1>
<p>My paragraph.</p>
</body>
</html>
If you want to know how to read a file, within a directory, and do something with it, here you go. This also shows you how to run a command through the power shell. This is in TypeScript! I had trouble with this, so I hope this helps someone one day. What this did for me was webpack all of my .ts files in each of my directories within a certain folder to get ready for deployment. Hope you can put it to use!
import * as fs from 'fs';
let path = require('path');
let pathDir = '/path/to/myFolder';
const execSync = require('child_process').execSync;
let readInsideSrc = (error: any, files: any, fromPath: any) => {
if (error) {
console.error('Could not list the directory.', error);
process.exit(1);
}
files.forEach((file: any, index: any) => {
if (file.endsWith('.ts')) {
//set the path and read the webpack.config.js file as text, replace path
let config = fs.readFileSync('myFile.js', 'utf8');
let fileName = file.replace('.ts', '');
let replacedConfig = config.replace(/__placeholder/g, fileName);
//write the changes to the file
fs.writeFileSync('myFile.js', replacedConfig);
//run the commands wanted
const output = execSync('npm run scriptName', { encoding: 'utf-8' });
console.log('OUTPUT:\n', output);
//rewrite the original file back
fs.writeFileSync('myFile.js', config);
}
});
};
// loop through all files in 'path'
let passToTest = (error: any, files: any) => {
if (error) {
console.error('Could not list the directory.', error);
process.exit(1);
}
files.forEach(function (file: any, index: any) {
let fromPath = path.join(pathDir, file);
fs.stat(fromPath, function (error2: any, stat: any) {
if (error2) {
console.error('Error stating file.', error2);
return;
}
if (stat.isDirectory()) {
fs.readdir(fromPath, (error3: any, files1: any) => {
readInsideSrc(error3, files1, fromPath);
});
} else if (stat.isFile()) {
//do nothing yet
}
});
});
};
//run the bootstrap
fs.readdir(pathDir, passToTest);
var fs = require('fs');
var path = require('path');
exports.testDir = path.dirname(__filename);
exports.fixturesDir = path.join(exports.testDir, 'fixtures');
exports.libDir = path.join(exports.testDir, '../lib');
exports.tmpDir = path.join(exports.testDir, 'tmp');
exports.PORT = +process.env.NODE_COMMON_PORT || 12346;
// Read File
fs.readFile(exports.tmpDir+'/start.html', 'utf-8', function(err, content) {
if (err) {
got_error = true;
} else {
console.log('cat returned some content: ' + content);
console.log('this shouldn\'t happen as the file doesn\'t exist...');
//assert.equal(true, false);
}
});