fs

How to access name of file within fs callback methods?

允我心安 提交于 2019-12-05 11:31:08
How do I get access to the the arguments of fs.read , fs.stat ... methods from within a callback? For instance if I want to process a file based on its size Following (coffeeScript) code snippet #assuming test1.txt exists filename = "./test1.txt" fs.stat filename, (err, stats) -> data = filename:filename,size:stats.size console.log data #further process filename based on size filename = "./test2.txt" prints { filename: './test2.txt', size: 5 } as filename is set to "./test2.txt". If I process/read the file using filename variable within fs.stat callback it would use test2.txt which is not

Web scraping with CasperJS returns strange error that isn't documented

做~自己de王妃 提交于 2019-12-05 07:14:28
I wrote an web scraping script with CasperJS and it works perfectly on Mac OS 10.10.4 with CasperJS version 1.1.0-beta3 and PhantomJS version 1.9.8, but when I put the same script on one of my servers which is Ubuntu 14.04 (running inside Docker container) with the same environment (CasperJS and PhantomJS all the same versions) it suddenly just outputs this: I'm `fs` modules Which is pretty strange. One of my suggestion is that in this script I am also trying to require some other scripts with require like that: var parsingStrategy = require(strategiesPath + strategyName); and the path to

Open local file in electron and render in wavesurfer.js

白昼怎懂夜的黑 提交于 2019-12-05 03:03:10
I'm working on an app built with electron, it should work with wavesurfer.js to display a waveform representing the audio file. However, I'm having trouble opening the file using the fs module and pushing the file content to wavesurfer via a Blob. The file loads and everything seems to work but when decoding wavesurfer says Error decoding audiobuffer . Two things I thought maybe could influence this: The fs.readFile function takes an encoding as second parameter The Blob constructor takes an options object as second parameter, whithin this you can define the mimetype via the type property

文件流

混江龙づ霸主 提交于 2019-12-05 02:53:50
var fs = require("fs"); var readstream = fs.createReadStream(__dirname + "\\stuff\\readMe.txt"); var writestream = fs.createWriteStream(__dirname + "\\stuff\\writeMe.txt") readstream.setEncoding("utf-8"); var str = ""; readstream.on("data", function (data) { str += data; writestream.write(data); //写入 }); readstream.on("end", function () { console.log(str); }); /* var readstream = fs.createReadStream(__dirname + "\\stuff\\readMe.txt"); var writestream = fs.createWriteStream(__dirname + "\\stuff\\writeMe.txt") readstream.pipe(writestream); */ 来源: https://www.cnblogs.com/lbx6935/p/11899727.html

node文件读写

廉价感情. 提交于 2019-12-05 02:46:59
var fs = require("fs"); var readme = fs.readFileSync("readMe.txt", "utf-8"); //同步读取 var writeme = fs.writeFileSync("writeMe.txt", "写入!"); //同步写入 console.log(readme); var readme = fs.readFile("readMe.txt", "utf-8", function (err, data) { //异步读取 fs.writeFile("writeMe.txt", "写入!", function () { //异步写入 console.log("writeme has finished") }) }) console.log("finish") 来源: https://www.cnblogs.com/lbx6935/p/11899363.html

how to determine whether the directory is empty directory with nodejs

荒凉一梦 提交于 2019-12-05 01:00:27
I have searched the Nodejs Doc,But don't find relative API. So I write the following code to determine whether the directory is empty directory. var fs = require('fs'); function isEmptyDir(dirnane){ try{ fs.rmdirSync(dirname) } catch(err){ return false; } fs.mkdirSync(dirname); return true } QUESTION:it look like some troublesome,there is better way to do it with nodejs? I guess I'm wondering why you don't just list the files in the directory and see if you get any files back? fs.readdir(dirname, function(err, files) { if (err) { // some sort of error } else { if (!files.length) { // directory

App base path from a module in NodeJS

99封情书 提交于 2019-12-04 22:15:41
I'm building a web app in NodeJS, and I'm implementing my API routes in separate modules. In one of my routes I'm doing some file manipulation and I need to know the base app path. if I use __dirname it gives me the directory that houses my module of course. I'm currently using this to get the base app path (given that I know the relative path to the module from base path): path.join(__dirname, "../../", myfilename) Is there a better way than using ../../ ? I'm running Node under Windows so there is no process.env.PWD and I don't want to be platform specific anyway. The approach of using _

How to create folder hierarchy of year -> month -> date if not exist in node.js

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 14:51:29
问题 I am trying to create folder hierarchy named current year inside create another folder with name current month and then again inside that folder create another folder with name current date. For example : Today's date is 2016-05-02, So the folder should be create if not already exist like following structure 2016->05->02 回答1: See this previously answered question Good way to do this is to use mkdirp module. $ npm install mkdirp Then use it to run function that requires the directory. Callback

How to check in Node.js if a file is open/being written to?

纵饮孤独 提交于 2019-12-04 10:18:21
I found many answers for C++, C#, etc. but haven't yet found one for Node.js. Here is my case. I've created a module which watches for file/directory changes (new or updated, doesn't care about deleted files) in my FTP server. Once I notice a new file, or an existing one is changed, I get a notification and notify my module B to sync this file to Sourceforge.net. The module works fine, reacts to changes instantly. But what I'm not satisfied with yet is that when a new file is added, that file receives "modified" event several times depending on the file size: the bigger the size, the more

readFileSync from an URL for Twitter media - node.js

余生颓废 提交于 2019-12-04 05:03:28
问题 I need to use an URL to import a media to Twitter. But I can't figure out how to use an URL without having to download the image, save it temporarily on the server and deleted after... I'm using Google Cloud to store the images that's why I can't access it directly. That's what I'm trying to do : var data = require('fs').readFileSync('https://www.example.com/image.png'); //Create the Twitter client const client = new Twitter({ consumer_key: process.env.consumer_key_dev, consumer_secret: