fs

Using fs.readdir and fs.statSync returns ENOENT, no such file or directory error

天涯浪子 提交于 2019-12-01 08:43:14
This works: var promise = new Future(), dirs = [], stat; Fs.readdir(Root + p, function(error, files){ _.each(files, function(file) { //stat = Fs.statSync(file); //if ( stat.isDirectory() ) { dirs.push(file); //} }); promise.return(dirs); }); This does not: var promise = new Future(), dirs = [], stat; Fs.readdir(Root + p, function(error, files){ _.each(files, function(file) { stat = Fs.statSync(file); if ( stat.isDirectory() ) { dirs.push(file); } }); promise.return(dirs); }); Resulting in "Error: ENOENT, no such file or directory 'fonts'" fonts is the first directory in the tree, and it does

Node.js fs.writeFile() empties the file

半城伤御伤魂 提交于 2019-12-01 07:47:27
问题 I have an update method which gets called about every 16-40ms, and inside I have this code: this.fs.writeFile("./data.json", JSON.stringify({ totalPlayersOnline: this.totalPlayersOnline, previousDay: this.previousDay, gamesToday: this.gamesToday }), function (err) { if (err) { return console.log(err); } }); If the server throws an error, the "data.json" file sometimes becomes empty. How do I prevent that? 回答1: Problem fs.writeFile is not an atomic operation. Here is an example program which I

Java API操作HDFS

别来无恙 提交于 2019-12-01 07:01:05
package project.etl.core.util; import java.io.FileNotFoundException; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileStatus; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.security.UserGroupInformation; /** * 操作HDFS工具类 * @author snow * @date: 2019-10-13 10:01:59 */ public class HDFSUtil {   /**   * 获取默认文件系统   * @return   * @throws IOException   * @author snow   * @date: 2019-10-13 10:16:18   */   public static FileSystem getFileSystem() throws IOException {     Configuration conf = new

Get all files recursively in directories NodejS

岁酱吖の 提交于 2019-12-01 03:18:31
I have a little problem with my function. I would like to get all files in many directories. Currently, I can retrieve the files in the file passed in parameters. I would like to retrieve the html files of each folder in the folder passed as a parameter. I will explain if I put in parameter "test" I retrieve the files in "test" but I would like to retrieve "test / 1 / *. Html", "test / 2 / . / .html ": var srcpath2 = path.join('.', 'diapo', result); function getDirectories(srcpath2) { return fs.readdirSync(srcpath2).filter(function (file) { return fs.statSync(path.join(srcpath2, file))

Can Multiple fs.write to append to the same file guarantee the order of execution?

≡放荡痞女 提交于 2019-11-30 19:46:16
Assume we have such a program: // imagine the string1 to string1000 are very long strings, which will take a while to be written to file system var arr = ["string1",...,"string1000"]; for (let i = 1; i < 1000; i++) { fs.write("./same/path/file.txt", arr[i], {flag: "a"}}); } My question is, will string1 to string1000 be gurantted to append to the same file in order? Since fs.write is async function, I am not sure how each call to fs.write() is really executed. I assume the call to the function for each string should be put somewhere in another thread (like a callstack ?) and once the previous

How can I lock a file while writing to it asynchronously

给你一囗甜甜゛ 提交于 2019-11-30 16:05:58
I've got two node threads running, one watching a directory for file consumption and another that is responsible for writing files to given directories. Typically they won't be operating on the same directory, but for an edge case I'm working on they will be. It appears that the consuming app is grabbing the files before they are fully written, resulting in corrupt files. Is there a way I can lock the file until the writing is complete? I've looked into the lockfile module, but unfortunately I don't believe it will work for this particular application. ===== The full code is far more than

Accessing filesystem in Angular 2 app using Electron

好久不见. 提交于 2019-11-30 13:47:27
I know that Angular 2 is run on a web browser, which does not have access to the file system. However, I'm using Electron as my front-end, and also running the app via electron: "build-electron": "ng build --base-href . && cp src/electron/* dist", "electron": "npm run build-electron && electron dist" Therefore, I run it with npm run electron which at the very end runs electron dist . Since I'm running through electron and not ng I would think that I should be able to access the filesystem. However, when I do: import * as fs from 'fs' I get an error: ng:///AppModule/AppComponent_Host.ngfactory

Saving an image stored on s3 using node.js?

限于喜欢 提交于 2019-11-30 13:37:13
I'm trying to write an image server that uses node.js to store images on s3. Uploading the image works fine, and I can download and view it correctly using an s3 browser client (I'm using dragondisk, specifically, but I've successfully downloaded it with other ones too), but when I download it with node and try to write it to disk, I'm unable to open the file (it says it may be damaged or use a file format that Preview does not recognize). I'm using the amazon sdk for node and fs to write the file. I know that you can pass an optional encoding to fs.writeFile, but I've tried them all and it

Node - Can't seek audio stream

萝らか妹 提交于 2019-11-30 13:21:12
问题 I have created a simple server that uses the fs module to stream an mp3 file to a browser which plays it in an html5 audio element. As it is, the audio streams perfectly fine, however, I can't seek through the stream, even if the part I seek to has already been buffered. var express = require('express'); var app = express(); var fs = require('fs'); app.get('/', function (req, res) { var filePath = 'music.mp3'; var stat = fs.statSync(filePath); res.writeHead(200, { 'Content-Type': 'audio/mpeg'

Node.js from fs.readFileSync() to fs.readFile()

寵の児 提交于 2019-11-30 11:46:56
问题 I'm trying to get my head around synchronous vs asynchronous in Node.js, in particular for reading an html file. In a request handler, the synchronous version that i'm using, which works is the following: var fs = require("fs"); var filename = "./index.html"; var buf = fs.readFileSync(filename, "utf8"); function start(resp) { resp.writeHead(200, {"Content-type":"text/html"}); resp.write(buf); resp.end(); } exports.start=start; What would be the version using readFile() ?? I understand that