fs

Download an image using node-request, and fs Promisified, with no pipe in Node.js

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-18 22:35:21
问题 I have been struggling to succeed in downloading an image without piping it to fs. Here's what I have accomplished: var Promise = require('bluebird'), fs = Promise.promisifyAll(require('fs')), requestAsync = Promise.promisify(require('request')); function downloadImage(uri, filename){ return requestAsync(uri) .spread(function (response, body) { if (response.statusCode != 200) return Promise.resolve(); return fs.writeFileAsync(filename, body); }) .then(function () { ... }) // ... } A valid

NodeJS笔记: 文件操作大全

断了今生、忘了曾经 提交于 2019-12-18 18:27:03
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 文件I/O fs模块的基本用法 开发中我们经常会有文件I/O的需求,node.js中提供一个名为fs的模块来支持I/O操作,fs模块的文件I/O是对标准POSIX函数的简单封装。 writeFile函数的基本用法 文件I/O,写入是必修课之一。fs模块提供writeFile函数,可以异步的将数据写入一个文件, 如果文件已经存在则会被替换。用法如下: 例: fs.writeFile(filename, data, callback) var fs= require("fs"); fs.writeFile('test.txt', 'Hello Node', function (err) { if (err) throw err; console.log('Saved successfully'); //文件被保存 }); appendFile函数的基本用法 writeFile函数虽然可以写入文件,但是如果文件已经存在,我们只是想添加一部分内容,它就不能满足我们的需求了,很幸运,fs模块中还有appendFile函数,它可以将新的内容追加到已有的文件中,如果文件不存在,则会创建一个新的文件。使用方法如下: 例: fs.appendFile(文件名,数据,编码,回调函数(err)); var fs= require(

How can I lock a file while writing to it asynchronously

怎甘沉沦 提交于 2019-12-18 18:24:24
问题 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

Accessing filesystem in Angular 2 app using Electron

╄→гoц情女王★ 提交于 2019-12-18 15:51:45
问题 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.

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

为君一笑 提交于 2019-12-18 05:08:18
问题 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

using promises in node.js to create and compare two arrays

故事扮演 提交于 2019-12-17 21:21:50
问题 I needed to compare two arrays the first one a couple of filenames from a database, the second one a list of files I already downloaded to my client. The Idea was to load whatever files are missing on the client. As the reading via fs was two slow, I tried using Promises to wait for one function to finish before the next starts. But somehow I got lost... My code so far: let filesIneed = []; let filesIhave = []; let filesToFetch = []; getLocalFiles().then(getFilesIneed).then(getfilesToRetreive

How do I use chmod with Node.js

人盡茶涼 提交于 2019-12-17 18:45:03
问题 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

Node.js check if file exists

痴心易碎 提交于 2019-12-17 17:25:25
问题 How do i check the existence of a file ? In the documentation for the module fs there's a description of the method fs.exists(path, callback). But, as I understand, it checks for the existence of only directories. And I need to check the file ! How can this be done? 回答1: Why not just try opening the file ? fs.open('YourFile', 'a', function (err, fd) { ... }) anyway after a minute search try this : var path = require('path'); path.exists('foo.txt', function(exists) { if (exists) { // do

Write / add data in JSON file using Node.js

纵饮孤独 提交于 2019-12-17 04:11:01
问题 I am trying to write JSON file using node from loop data, e.g.: let jsonFile = require('jsonfile'); for (i = 0; i < 11; i++) { jsonFile.writeFile('loop.json', "id :" + i + " square :" + i * i); } outPut in loop.json is: id :1 square : 1 but I want output file like this (below) and also if I run that code again it should add that new output as elements in same existing JSON file: { "table":[ { "Id ":1, "square ":1 }, { "Id ":2, "square ":3 }, { "Id ":3, "square ":9 }, { "Id ":4, "square ":16 }

Return array from node.js readline module on close event

女生的网名这么多〃 提交于 2019-12-14 03:53:51
问题 I'm calling a function on the server side that opens a csv file and searches for a string in each line. On the close event, the function should return an array that contains the first 5 string matches from the csv file (in the first column). However, it seems the array is unaccessible outside the function (possibly due to asynchronous behaviour): index.js function calling_function() { var a_string = "foo"; var array = database_search(a_string); console.log(array); } function database_search(a