I set up my discord BOT using node.js. For my advantage, I would need to store some data on a external file, but I don't seem to be able to access it from my index.js file (the main Bot file).
I've tried having one static array in the external js/json files, but I can only retrieve undefined/empty values. Additionally, when I tried with a .txt file, once retrieved the content, I found it unable to call functions such as string.split().
Did I miss something in the package content perhaps?
Assuming the data you are storing is in UTF-8 encoding:
var fs = require('fs');
fs.readFile('path/to/file', 'utf8', function(err, contents) {
// code using file data
});
Assuming no errors contents will be a string of the data that is inside that file.
https://code-maven.com/reading-a-file-with-nodejs
Related
first, I want to upload .xlxs and .csv file in node.js. then I want to read this file, edit this file, and also want to save this file in mongoose schema in the database.
and I am new in node.js and I want to learn node backend
please guide me, how I can do this and is this possible?
Reading and writing files in node uses the fs module. Its already included in core node so simply include it
const fs = require('fs')
// synchronous version that will wait until the whole file is read
let file_data = fs.readFileSync('/file/path/file.csv' , 'utf-8')
// async version
fs.readFile('/file/path/file.csv' , 'utf-8', (err, file_data) => {
if (err)
console.error(err)
else
console.info(file_data)
// file_data is your data as string
})
If you specifically need to read and edit csv/xlsx you'll need to browse NPM for packages that help out with that.
You can do it easily by using sheetjs
See this example : https://github.com/SheetJS/sheetjs/tree/master/demos/server
I am trying to create a zip file in node using the code provided from how to create a zip file in node given multiple downloadable links, as shown below:
var fs = require('fs');
var archiver = require('archiver');
var output = fs.createWriteStream('./example.zip');
var archive = archiver('zip', {
gzip: true,
zlib: { level: 9 } // Sets the compression level.
});
archive.on('error', function(err) {
throw err;
});
// pipe archive data to the output file
archive.pipe(output);
// append files
archive.file('/path/to/file0.txt', {name: 'file0-or-change-this-whatever.txt'});
archive.file('/path/to/README.md', {name: 'foobar.md'});
//
archive.finalize();
When I use this suggestion, the zip file is downloaded without any kind of prompt asking me where I would like to save the file - is there any way I can make it so that a prompt is created asking me where I would like to save the file, which is quite normal these days?
If this is absolutely not possible, would it be possible to always save the file in the downloads folder (regardless of whether on mac or windows or any other operating system)?
So there's a couple of things here. In terms of a 'prompt' or 'pop-up' you won't find anything along the lines of WinForms out of the box, there are options for the command line such as prompts You can use that as your user input.
https://www.npmjs.com/package/prompts
You'll want to use path and more specifically path.join() to combat the mac/windows/linux issue.
Do you need to use path.join in node.js?
You can run an express server and create a route that uses res.download() in which you would provide the zipped file.
https://expressjs.com/en/api.html#res.download
How can i search key words into a text and return the data just after this key word with node js i find it with php and c# but not with javascript or node js?
We can implement the above scenario using node's fs module.With the help of this we can read the file's data and perform the keyword search operations on the processed data.
var fs = require("fs")
fs.readFile("somefile.txt", function(err, content) {
if (err) throw err;
// Do Required Operations with the content which is the file data
});
Take a look at npm config.
It sounds like you are trying to parse an ini file, or something like that. If so, you can use the .properties file format.
So assuming you store your text file as config/default.properties, you could do something like:
const config = require('config');
const MYVALUE = config.get('MYKEY');
I'm trying to access data in node.js from an Excel(csv, xls) file stored inside Azure blob. I'm using follow code in node.js
Var workbook = Obj.fromStream(request.get('excel location url'))
I'm getting nothing through this.
Note : Excel file is open, no authentication required.
First, the csvtojson library does not support Excel file, only csv.
Look at the documentation, you can read each line using json, line and csv output.
But your code should look like that:
const request=require('request')
const csv=require('csvtojson')
const http = require('http');
csv()
.fromStream(request.get('csv file url'))
.subscribe((json)=>{
console.log(json)
});
I'm a front-end dev just venturing into the Node.js, particularly in using it to create small command line tools.
My question: how do you persist data with command line tools? For example, if I want to keep track of the size of certain files over time, I'd need to keep a running record of changes (additions and deletions) to those files, and relevant date/time information.
On the web, you store that sort of data in a database on a server, and then query the database when you need it again. But how do you do it when you're creating a Node module that's meant to be used as a command line tool?
Some generic direction is all I'm after. I don't even know what to Google at this point.
It really depends on what you're doing, but a simple approach is to just save the data that you want to persist to a file and, since we're talking node, store it in JSON format.
Let's say you have some data like:
var data = [ { file: 'foo.bar', size: 1234, date: '2014-07-31 00:00:00.000'}, ...]
(it actually doesn't matter what it is, as long as it can be JSON.stringifiy()d)
You can just save it with:
fs.writeFile(filename, JSON.stringify(data), {encoding: 'utf8'}, function(err) { ... });
And load it again with:
fs.readFile(filename, {encoding: 'utf8'}, function(err, contents) {
data = JSON.parse(contents);
});
You'll probably want to give the user the ability to specify the name of the file you're going to persist the data to via an argument like:
node myscript.js <data_file>
You can get that passed in parameter with process.argv:
var filename = process.argv[2]; // Be sure to check process.argv.length and have a default
Using something like minimist can be really helpful if you want to get more complex like:
node myscript.js --output <data_file>
You also can store files in temporary directory, for example /tmp directory on linux and give user the option to change the directory.
To get path to temporary directory you can use os module in nodejs:
const os = require('os');
const tmp = os.tmpdir();