NodeJS, fs.readFileSync string-by-string reading, and operating. How? - node.js

Sorry for so dumb question,
How i can in NodeJS read from file string by string some value, for example - url, and do operation with each string eventually?
var contents = fs.readFileSync('test.txt', 'utf8');
and what then?
It's need for browserstack+selenium testing.
I want run some links one-by-one, from file and do something with them.
Changed code below:
from
console.log(lines[i++])
to
line = (lines[i++])
driver.get(line);
driver.getCurrentUrl()
.then(function(currentUrl) {
console.log(currentUrl);
But it works once.
And
var str=fs.readFileSync('test.txt');
str.split(/\n/).forEach(function(line){})
C:\nodejstest>node test1.js
C:\nodejstest\test1.js:57
str.split(/\n/).forEach(function(line){
^
TypeError: str.split is not a function
at Object.<anonymous> (C:\nodejstest\test1.js:57:5)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:142:18)
at node.js:939:3
Works!
Much thanx!

Another (simpler) method would be to read the entire file into a buffer, convert it to a string, split the string on your line-terminator to produce an array of lines, and then iterate over the array, as in:
var buf=fs.readFileSync(filepath);
buf.toString().split(/\n/).forEach(function(line){
// do something here with each line
});

read file with readable stream and do your operation when you find '\n'.
var fs=require('fs');
var readable = fs.createReadStream("data.txt", {
encoding: 'utf8',
fd: null
});
var lines=[];//this is array not a string!!!
readable.on('readable', function() {
var chunk,tmp='';
while (null !== (chunk = readable.read(1))) {
if(chunk==='\n'){
lines.push(tmp);
tmp='';
// this is how i store each line in lines array
}else
tmp+=chunk;
}
});
// readable.on('end',function(){
// console.log(lines);
// });
readable.on('end',function(){
var i=0,len=lines.length;
//lines is #array not string
while(i<len)
console.log(lines[i++]);
});
See if it works for you.

Related

Node.js - Function not working when exported

Good day everyone,
First off, thanks for always being such an amazing community. You all really are helping me a ton with learning and bettering my programming and development!
I have a small question related to the module.exports within Node.js. The function below runs with no issues when called on directly:
const fs = require('fs')
const {nanoid} = require('nanoid')
const createStormDB = () => {
return new Promise((resolve, reject) => {
try{
const id = nanoid(4)
const date = new Date() // Create date string for file naming
let dateString = `${date.toISOString().split('T')[0]}` // Create date string for file naming
let fileName = `${dateString}_deals_${id}.stormdb` // Create date string for file naming
fs.openSync(`../StormDB/${fileName}`, 'w')
resolve(fileName)
}catch(err){
reject(err)
}
})
}
module.exports = createStormDB
It creates a file with a specific name within in specific folder. But when I use module.exports = createStormDB I am greeted with the following error:
(node:12516) UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory, open '../StormDB/2021-07-19_deals_gYmJ.stormdb'
at Object.openSync (fs.js:476:3)
at C:\Node\Pipedrive-Connector\PipeDriveRequest\scripts\createStormDBFile.js:11:16
at new Promise (<anonymous>)
at createStormDB (C:\Node\Pipedrive-Connector\PipeDriveRequest\scripts\createStormDBFile.js:5:12)
at Object.<anonymous> (C:\Node\Pipedrive-Connector\PipeDriveRequest\play.js:7:1)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
Is there something I am misunderstanding when it comes to exporting modules? I'm importing this module using the require option! Thanks so much for the help!
The .. relative path in the function is relative to the calling scripts current working directory, not the directory the file is in.
Assuming from your path setup and description the database is in: C:\Node\Pipedrive-Connector\PipeDriveRequest\StormDB
If you want the database path to remain relative to the javascript file containing the function, use __dirname
const path = require('path')
const db_path = path.join(__dirname, '..', 'StormDB', filename)
fs.openSync(db_path, 'w')

Read file and write file JSON

In this, I am trying to make a hit counter where every time someone visits my site the variable will be read from the views.json file one is added to the number and then the .json will be updated with the new number. However when I tested it in a repl.it project I got an error saying
ReferenceError: writeFileSync is not defined
at /home/runner/hit-counter/index.js:6:1
at Script.runInContext (vm.js:133:20)
at Object.<anonymous> (/run_dir/interp.js:156:20)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32) at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
I don't know what this means if you know please tell me and how I may be able to fix it.
the reply project link:https://hit-counter.cohense.repl.run/
The JavaScript (ES6)
const fs = require('fs');
let views = fs.readFileSync('views.json');
views = JSON.parse(views);
views.total++;
let data = JSON.stringify(views, null, 2);
writeFileSync("views.json", data, finished);
function finished(err) {
if (views = JSON.parse(views)) {
console.log("Your view has been accounted for!")
} else {
console.error("Error occured please reload the page =(")
}
};
the JSON
{
"totalViews": 1
}
You can do like this, just fixed some errors.
Oh, you should use writeFileSync, to avoid that the file will not be edited at same time.
The question is, why don't you use a DB? It's a lot faster and fix concurrency writes.
var fs = require('fs')
var data = fs.readFileSync('views.json')
var views = JSON.parse(data);
console.log(views);
views.total = views.total + 1;
var data = JSON.stringify(views, null, 2)
writeFileSync("views.json", data, ()=>{
console.log("Your View Has Been Accounted For!")
})
I found out what I did wrong I didn't use fs.
writeFileSync("views.json", data, finished);
When I just needed to do
fs.writeFileSync("views.json", data[,finished]);

How to encode a node gd image to base 64 without saving the file?

I am manipulating some images using node-gd, trying to upload to twitter via their api.
Doing it like so works fine:
fs.readFileSync('test.png', { encoding: 'base64' })
But I was hoping there was a way to do this without saving it to a file then loading it again.
Other people suggest doing something like this:
var gd = require('node-gd');
var img = gd.openPng('test.png');
var buff = Buffer.from(img.pngPtr(), 'binary');
var b64 = buff.toString('base64');
console.log(b64);
But when I do this, I get an error saying TypeError: binary is not a function which makes no sense to me.
Full error:
/home/ubuntu/workspace/test.js:16
var buff = Buffer.from(img.pngPtr(), 'binary');
^
TypeError: binary is not a function
at Function.from (native)
at Function.from (native)
at Object.<anonymous> (/home/ubuntu/workspace/test.js:16:19)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:968:3
It seems buffer.from() is relatively new (v 5.10.0), here is the syntax for earlier versions:
new Buffer(pngFile.pngPtr(), 'binary')
A bit late but nevertheless:
var gd = require('node-gd');
var img = gd.openPng('test.png');
var buff = img.pngPtr(-1, true);
var b64 = buff.toString('base64');
console.log(b64);
Where for img.pngPtr(-1, true), -1 is default compression and true will return a Buffer.

Readable Stream Node : the _read method with the Number of bytes param

I am trying to figure out streams in node and playing around with some examples in the stream handbook
I am trying out the _read method of a readable stream. It says, it takes in a parameter which is the number of bytes the consumer wants to read.
I have two questions here.
Is the number of bytes the consumer wants to read the 'watermark'
Why do i get an error when I use _read with a parameter.
This is my code.
var Readable = require('stream').Readable;
var rs = Readable();
var c = 97;
rs._read = function (5) {
rs.push(String.fromCharCode(c++));
if (c > 'z'.charCodeAt(0)) {
rs.push('\n');
rs.push(null);
}
};
setTimeout(function () {
rs.pipe(process.stdout);
}, 2000);
And this is the error
/Users/nikhilkuria/Dev/git/node_demo/streams/streamRead.js:5
rs._read = function (5) {
^
SyntaxError: Unexpected number
at Object.exports.runInThisContext (vm.js:76:16)
at Module._compile (module.js:542:28)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3
function (5) { is not valid syntax for creating a function. Instead of a number, you'll want to pass in a variable name, like this: function (a) {
The error you're seeing is due to JavaScript not allowing variable names starting with or consisting of only numbers.

Exception when attempting to wrap Buffered-writer

Disclaimer: I'm fairly new to node (but not to JavaScript).
Im just trying to write a logger class, which holds a few lines at a time in memory, then flushes to disk when its buffer is reaching capacity
Problem
Calling my logger wrapper results in an exception in buffered writer
Im sure Ive misunderstood how require() works, and also various people advised me to create the object using new chatlogs.Chatlogger() but itI dont see many other node libs using this way of working
/www/im/node_modules/buffered-writer/lib/buffered-writer.js:125
cb ();
^
TypeError: undefined is not a function
at Writer.flush (/www/nodeim/node_modules/buffered-writer/lib/buffered-writer.js:125:3)
at Chatlogger.close (/www/nodeim/helpers/chatlogs.js:27:14)
at Object.<anonymous> (/www/nodeim/app.js:76:16)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3
calling code...
var chatlogs = require('./helpers/chatlogs.js');
var chatlogger_obj = new chatlogs.Chatlogger();
chatlogger_obj.open("logs/log.txt");
chatlogger_obj.log("TESTING");
chatlogger_obj.close();
process.exit(0);
Wrapper class
./helpers/chatlogs.js
exports.version = '0.0.1';
var
buffer = require('buffered-writer'),
fs = require('fs');
var Chatlogger = function() {
this.handle = null,
this.filename = "",
this.dirtyops = 0;
}
Chatlogger.prototype.open = function (filename) {
//fs.unlink(filename);
this.filename = filename;
this.handle = buffer.open(filename)
.on ("error", function (error) {
//this.handle = null;
console.log (error);
});
}
Chatlogger.prototype.close = function() {
console.log("CLOSING");
this.handle.flush();
this.handle.close();
this.handle = null;
}
Chatlogger.prototype.log = function (str) {
console.log(str);
this.handle.writeln(str);
if (this.dirtyops++ > 5)
{
console.log("FLUSHING");
this.handle.flush();
this.dirtyops = 0;
}
}
module.exports.Chatlogger = Chatlogger;
I'm the author of this module. You need to pass a callback to the flush function, but you don't need to call to flush. When the buffered-writer closes or you exceed the buffer size when writing, the data is automatically flushed to disk.
Writer#flush(callback)

Resources