fs

Swap order of arguments to “then” with Bluebird / NodeJS Promises

房东的猫 提交于 2019-12-12 03:06:22
问题 I have a function which asynchronously grabs a value from a server: var request = require('request'); Promise.promisifyAll(request); function getValue(){ return request.getAsync('http://www.google.com') .then(function(resp){ return resp.body; }) .catch(function(err){ thow err; }); } I want to take this value and dump it to a file: var fs = require('fs'); Promise.promisifyAll(fs); getValue().then(fs.writeFileAsync, "file.html"); The problem is that fs.writeFileAsync expects parameter one to be

Reading a .txt file with NodeJS using FS

▼魔方 西西 提交于 2019-12-12 01:25:28
问题 I'm trying to use NodeJS to read a txt file by using fs. This is the code of app.js: var fs = require('fs'); function read(file) { return fs.readFile(file, 'utf8', function(err, data) { if (err) { console.log(err); } return data; }); } var output = read('file.txt'); console.log(output); When i do: node app.js It says undefined I have fs installed and there is a file.txt in the same directory, why is it not working? 回答1: Your read function is returning the result of the fs.readFile function,

echosign combinedDocument api

徘徊边缘 提交于 2019-12-11 23:37:04
问题 https://api.na1.echosign.com/api/rest/v5/agreements/{agreementId}/combinedDocument I am trying to create a file from the body of the response, but it is creating a file that I can't open. It requires a password even though there isn't one on the file. I think this must have something to do with the encoding / decoding. I am using a node express server. Here are the few lines of code I am using: var request = require('request'); request({ baseUrl: 'https://api.na1.echosign.com/api/rest/v5',

Events for fs createReadStream Not Always Being Called

試著忘記壹切 提交于 2019-12-11 23:19:52
问题 I have an fs filestream being used to serve file downloads which are dynamically created. I get this stream and pipe it to my response object after setting appropriate headers. I also set a couple stream events so that if there's an error or the stream ends, it removes the generated files from the file system. I'm running into an issue where, on occasion, when the download isn't correctly initialized or finished there may be some hanging files left on the file system. I believe this is

How to efficiently read file names when there are > 1 million files in directory (in Bash)

我的未来我决定 提交于 2019-12-11 21:26:12
问题 I am running find . -exec stat -f '%N,%a' {} + in a directory with > 1 million files. It just hangs and uses CPU. I left it running for over 5 minutes and still no output. I don't want it to crash my computer. What other ways are there to read the directory files in a way that doesn't load the whole thing into memory. I can't change the directory structure, I need it to have these > 1 million files in a flat directory. 来源: https://stackoverflow.com/questions/56204321/how-to-efficiently-read

export DOM Node to GIF with gif-encoder and dom-to-image libs

为君一笑 提交于 2019-12-11 17:16:26
问题 I am desperately trying to get a non-animated gif export from a DOM Node, on client-side, but without any success yet. I use for that the lib dom-to-image which, thanks to it's method toPixelData , return some pixels. Then I want these pixels to give me a gif. I use for this the lib gif-encoder which sounds great. I thought about using a Promise for that because the last step is to put this gif in a ZIP file created by JSZip. Here is the code below. Consider that I already have the DOM Node

Read each line of a txt file by looking for the first word at the beginning of each line and delete the line of file

冷暖自知 提交于 2019-12-11 16:43:19
问题 Imagine a txt file like this: Toto1 The line Toto2 The line Toto3 The line ... I would like to get the whole line of "Toto2" (or other like Toto120), and if the line exists then you have to remove it from the txt file The txt file will be of this form after: Toto1 The line Toto3 The line .... Do you have an idea? It is better to use the "fs" system of NodeJs; it is for the server side. Thank 回答1: Using fs is definitely the right way to go, as well as using RegExp to find the string you want

Requests with request-promise pipe result (Large data)

╄→гoц情女王★ 提交于 2019-12-11 15:53:14
问题 I try to download some data from an external API. I would like to pipe the response of every request. The array including the request URLs looks like this : [ 'https://scihub.copernicus.eu/dhus/odata/v1/Products(\'d98b8730-846f-46d0-a816-5ae4db9f56a7\')/$value', 'https://scihub.copernicus.eu/dhus/odata/v1/Products(\'6edaeb16-3077-45d1-b3f0-fa2d5549f64a\')/$value', 'https://scihub.copernicus.eu/dhus/odata/v1/Products(\'333db2aa-c695-4753-8bd1-e64308af26e1\')/$value', 'https://scihub.copernicus

How Do I Use The Replacer Function With JSON Stringify?

删除回忆录丶 提交于 2019-12-11 14:58:46
问题 I've been trying to use const fs = require("fs"); const settings = require("./serversettings.json") let reason = args.join(' '); function replacer(key, value) { return reason; } fs.writeFileSync(settings, JSON.stringify(settings.logchannel, replacer)) It seems like to me that it doesn't work, so I'm trying to figure out how replacers work as MDN made me even more confused. 回答1: The replacer function takes a key and a value (as it passes through the object and its sub objects) and is expected

Cannot mock filesystem in nodejs unit tests

会有一股神秘感。 提交于 2019-12-11 13:47:31
问题 Overview I have a simple module written in nodejs that uses fs-extra package to test if a file exists. The module throws when the path exists and proceed to next procedure otherwise. Here is the source file: // - main.js - import fs from 'fs-extra' export default async (pathName) => { // Do not procceed if path already exists. if (await fs.pathExists(projectPath)) { throw new Error(chalk.red.bold(`${projectPath} already exists`)) } // more logic here } I want to write a unit test that tests