fs

Node: fs write() doesn't write inside loop. Why not?

浪尽此生 提交于 2019-11-29 07:29:56
I want to create a write stream and write to it as my data comes in. However, I am able to create the file but nothing is written to it. Eventually, the process runs out of memory. The problem, I've discovered is that I'm calling write() whilst inside a loop. Here's a simple example: 'use strict' var fs = require('fs'); var wstream = fs.createWriteStream('myOutput.txt'); for (var i = 0; i < 10000000000; i++) { wstream.write(i+'\n'); } console.log('End!') wstream.end(); Nothing ever gets written, not even hello. But why? How can I write to the file within a loop? To supplement @MikeC's

Asynchronously reading and caching multiple files in nodejs

三世轮回 提交于 2019-11-28 22:08:20
I have an array which keeps URL of several files. For example: var files = ['1.html', '2.html', '3.html']; I need to read them asynchronously and save them in an object named cache (cache = {}). To do this I used the code: for(var i = 0; i < files.length; i++){ require('fs').readFile(files[i], 'utf8', function (error,data) { cache[files[i]]=data; }); } In the end I have the result: cache = { undefined : 'File 3 content' } I do understand that the "readFile" acts after the loop is ended and it looses it's scope. Is there a way to fix this or another method to read files from an array and cache

Looping through files in a folder Node.JS

旧巷老猫 提交于 2019-11-28 16:59:11
问题 I am trying to loop through and pick up files in a directory, but I have some trouble implementing it. How to pull in multiple files and then move them to another folder? var dirname = 'C:/FolderwithFiles'; console.log("Going to get file info!"); fs.stat(dirname, function (err, stats) { if (err) { return console.error(err); } console.log(stats); console.log("Got file info successfully!"); // Check file type console.log("isFile ? " + stats.isFile()); console.log("isDirectory ? " + stats

read file from aws s3 bucket using node fs

跟風遠走 提交于 2019-11-28 16:35:06
问题 I am attempting to read a file that is in a aws s3 bucket using fs.readFile(file, function (err, contents) { var myLines = contents.Body.toString().split('\n') }) I've been able to download and upload a file using the node aws-sdk, but I am at a loss as to how to simply read it and parse the contents. Here is an example of how I am reading the file from s3: var s3 = new AWS.S3(); var params = {Bucket: 'myBucket', Key: 'myKey.csv'} var s3file = s3.getObject(params) 回答1: You have a couple

Node.js check if path is file or directory

浪尽此生 提交于 2019-11-28 15:06:09
I can't seem to get any search results that explain how to do this. All I want to do is be able to know if a given path is a file or a directory (folder). Jason Sperske fs.lstatSync(path_string).isDirectory() should tell you. From the docs : Objects returned from fs.stat() and fs.lstat() are of this type. stats.isFile() stats.isDirectory() stats.isBlockDevice() stats.isCharacterDevice() stats.isSymbolicLink() (only valid with fs.lstat()) stats.isFIFO() stats.isSocket() NOTE: The above solution will throw an Error if; for ex, the file or directory doesn't exist. If you want a truthy or falsy

What are the pros and cons of fs.createReadStream vs fs.readFile in node.js?

僤鯓⒐⒋嵵緔 提交于 2019-11-28 13:50:47
问题 I'm mucking about with node.js and have discovered two ways of reading a file and sending it down the wire, once I've established that it exists and have sent the proper MIME type with writeHead: // read the entire file into memory and then spit it out fs.readFile(filename, function(err, data){ if (err) throw err; response.write(data, 'utf8'); response.end(); }); // read and pass the file as a stream of chunks fs.createReadStream(filename, { 'flags': 'r', 'encoding': 'binary', 'mode': 0666,

How do I use chmod with Node.js

扶醉桌前 提交于 2019-11-28 08:53:14
How do I use chmod with Node.js? There is a method in the package fs , which should do this, but I don't know what it takes as the second argument. fs.chmod(path, mode, [callback]) Asynchronous chmod(2). No arguments other than a possible exception are given to the completion callback. fs.chmodSync(path, mode) Synchronous chmod(2). (from the Node.js documentation ) If I do something like fs.chmodSync('test', 0755); nothing happens (the file isn't changed to that mode). fs.chmodSync('test', '+x'); doesn't work either. I'm working on a Windows machine btw. qiao according to its sourcecode /lib

Writing multiple files a loop in Nodejs

六眼飞鱼酱① 提交于 2019-11-28 05:46:04
问题 Hi I'm using a for each loop to in nodejs script to write multiple files to a local location.For the courseTitleArray I'm using "Biology 101,ruby" and I can write one file successfully but not the both .Please help me out. Here is my code so far for (var value in CourseTitleArray) { console.log( "Course Title " + CourseTitleArray[value]); var newImageLocation = path.join(__dirname, 'app/img/courseImages', CourseTitleArray[value] + ".png"); fs.readFile(image.path, function(err, data) { fs

Node.js Write a line into a .txt file

寵の児 提交于 2019-11-28 05:45:47
I want to create a simple Log System, which prints a line before the past line into a txt file by using Node.js, but i dont know how the File System from Node.js works. Can someone explain it ? Inserting data into the middle of a text file is not a simple task. If possible, you should append it to the end of your file. The easiest way to append data some text file is to use build-in fs.appendFile(filename, data[, options], callback) function from fs module : var fs = require('fs') fs.appendFile('log.txt', 'new data', function (err) { if (err) { // append failed } else { // done } }) But if you

Mock fs function with jest

China☆狼群 提交于 2019-11-28 04:04:33
问题 First of all, I'm new to es6 and jest . I have a Logger class for instantiate winston and I would like to test it. Here my code : const winston = require('winston'); const fs = require('fs'); const path = require('path'); const config = require('../config.json'); class Logger { constructor() { Logger.createLogDir(Logger.logDir); this.logger = winston.createLogger({ level: 'info', format: winston.format.json(), transports: [ new (winston.transports.Console)({ format: winston.format.combine(