fs

Why is fs.createReadStream … pipe(res) locking the read file?

帅比萌擦擦* 提交于 2019-12-07 15:55:35
问题 I'm using express to stream audio & video files according to this answer. Relevant code looks like this: function streamMedia(filePath, req, res) { // code here to determine which bytes to send, compute response headers, etc. res.writeHead(status, headers); var stream = fs.createReadStream(filePath, { start, end }) .on('open', function() { stream.pipe(res); }) .on('error', function(err) { res.end(err); }) ; } This works just fine to stream bytes to <audio> and <video> elements on the client.

Remove last n lines from file using nodejs

不羁的心 提交于 2019-12-07 12:50:18
问题 I'm trying to remove the last 3 lines from a file using fs as part of nodejs. I'm currently reading the file into memory and then writing it again without the 3 lines, but I'm sure there is a more efficient way that doesn't involve reading the whole file into memory. My code now fs.readFile(filename, function (err, data) { if (err) throw err; theFile = data.toString().split("\n"); theFile.splice(-3, 3); fs.writeFile(filename, theFile.join("\n"), function (err) { if (err) { return console.log

How to follow a (changing) log file in node.js

时光怂恿深爱的人放手 提交于 2019-12-07 10:31:24
问题 OK this might appear to be an easy question but I couldn't find the answer from here so I am posting it in hope someone might have encountered the similar problem. I need to monitor a symlink which points to a web server file ( /var/log/lighttpd/error.log to be more specific, thanks to Linus G Thiel I figured out how to follow symlinks). I know I can set up fs.fileWatch to monitor it but I should also point out that the error.log file also got rotated at a specific time, depending on whatever

How to access name of file within fs callback methods?

烂漫一生 提交于 2019-12-07 05:58:00
问题 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

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

蹲街弑〆低调 提交于 2019-12-07 02:13:38
问题 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

process.env.PWD vs process.cwd()

依然范特西╮ 提交于 2019-12-07 00:44:12
问题 I am using Meteor JS...and within my Meteor app I am using node to query the contents of different directories within the app.... When I use process.env.PWD to query the contents of a folder I get a different result from when I use process.cwd() to query the results of a folder. var dirServer = process.env.PWD + '/server/'; var dirServerFiles = fs.readdirSync(dirServer); console.log(dirServerFiles); //outputs: [ 'ephe', 'fixstars.cat', 'sepl_30.se1', 'server.js' ] vs var serverFolderFilesDir

What is `process.binding('fs')` in `fs.js`? [duplicate]

有些话、适合烂在心里 提交于 2019-12-07 00:21:10
问题 This question already has answers here : Nodejs: What does `process.binding` mean? (3 answers) Closed 3 years ago . I see in top of the fs.js there is a process.binding('fs') . https://github.com/nodejs/node/blob/master/lib/fs.js#L10: const binding = process.binding('fs'); And then, it's used as: binding.open(pathModule._makeLong(path), stringToFlags(flag), 0o666, req); (In https://github.com/nodejs/node/blob/master/lib/fs.js#L303-L306) My question is: What does process.binding('fs') mean?

NodeJS: Confusion about async “readdir” and “stat”

余生长醉 提交于 2019-12-07 00:12:34
In the docs it shows two versions of readdir and stat . Both of which have an async and sync version readir/readdirSync and stat/statSync . Because readidir and stat are async I would expect them to return a Promise but when trying to use async/await the script doesnt wait for readdir to resolve and if I use .then/.catch I get an error cannot read .then of undefined . All I'm trying to do here is map the directories that exist inside of the directory the script is being ran inside of to the dirsOfCurrentDir map. Returns error cannot read .then of undefined const fs = require('fs'); const

Read file with fs.readFileSync and eval contents…which scope have the functions? How to access?

[亡魂溺海] 提交于 2019-12-06 23:41:38
问题 I recently tried to import a file into my existing node.js project. I know this should be written with a module but i include my external javascript file like this: eval(fs.readFileSync('public/templates/simple.js')+'') The contents of simple.js looks like this: if (typeof examples == 'undefined') { var examples = {}; } if (typeof examples.simple == 'undefined') { examples.simple = {}; } examples.simple.helloWorld = function(opt_data, opt_sb) { var output = opt_sb || new soy.StringBuilder();

App base path from a module in NodeJS

若如初见. 提交于 2019-12-06 18:09:39
问题 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