fs

NodeJS accessing file with relative path [duplicate]

谁说胖子不能爱 提交于 2019-11-27 00:49:52
问题 This question already has answers here : Proper way to reference files relative to application root in Node.JS (3 answers) Closed 2 years ago . It seemed like a straight forward problem. But I amn't able to crack this. Within helper1.js I would like to access foobar.json (from config/dev/ ) root -config --dev ---foobar.json -helpers --helper1.js I couldn't get this to work fs: how do I locate a parent folder? Any help here would be great. 回答1: You can use the path module to join the path of

Using filesystem in node.js with async / await

房东的猫 提交于 2019-11-27 00:09:02
问题 I would like to use async/await with some filesystem operations. Normally async/await works fine because I use babel-plugin-syntax-async-functions . But with this code I run into the if case where names is undefined: import fs from 'fs'; async function myF() { let names; try { names = await fs.readdir('path/to/dir'); } catch (e) { console.log('e', e); } if (names === undefined) { console.log('undefined'); } else { console.log('First Name', names[0]); } } myF(); When I rebuild the code into

序列化

耗尽温柔 提交于 2019-11-26 21:17:09
private void btnSe_Click( object sender, EventArgs e) { computer com1 = new computer{ Color = " black " ,price = " 4000 " }; FileStream fs = new FileStream( @" D:\hhh.txt " , FileMode.Append); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(fs, com1); fs.Flush(); fs.Close(); } private void btnDe_Click( object sender, EventArgs e) { FileStream fs = new FileStream( @" D:\hhh.txt " ,FileMode.Open); BinaryFormatter bf = new BinaryFormatter(); computer com = (computer)bf.Deserialize(fs); MessageBox.Show(com.Color + " " + com.price); } private void Form1_Load( object sender, EventArgs e) {

write/add data in JSON file using node.js

不打扰是莪最后的温柔 提交于 2019-11-26 21:16:57
I am trying to write JSON file using node from loop data eg var 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 }, { "Id ": 5, "square ": 25 }, { "Id ": 6, "square ": 36 }, { "Id ": 7, "square ": 49 }, { "Id ": 8,

Find absolute base path of the project directory

有些话、适合烂在心里 提交于 2019-11-26 19:28:31
问题 Until now we could get the absolute path of a file to open later as readStream with this code snippet: var base = path.resolve('.'); var file = base + '/data/test.csv'; fs.createReadStream(file) Since Meteor 0.6.5 the base path is pointing to .meteor/local/build/programs/... There is also the Assets API, which but can not give us back a path but only the read document. We but need a stream to process some bigger data files? 回答1: Another way to find your project's root directory now is this:

How to close a readable stream (before end)?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 17:28:22
问题 How to close a readable stream in Node.js? var input = fs.createReadStream('lines.txt'); input.on('data', function(data) { // after closing the stream, this will not // be called again if (gotFirstLine) { // close this stream and continue the // instructions from this if console.log("Closed."); } }); This would be better than: input.on('data', function(data) { if (isEnded) { return; } if (gotFirstLine) { isEnded = true; console.log("Closed."); } }); But this would not stop the reading process

How to create full path with node&#39;s fs.mkdirSync?

安稳与你 提交于 2019-11-26 15:27:06
问题 I'm trying to create a full path if it doesn't exist. The code looks like this: var fs = require('fs'); if (!fs.existsSync(newDest)) fs.mkdirSync(newDest); This code works great as long as there is only one subdirectory (a newDest like 'dir1') however when there is a directory path like ('dir1/dir2') it fails with Error: ENOENT, no such file or directory I'd like to be able to create the full path with as few lines of code as necessary. I read there is a recursive option on fs and tried it

nodejs get file name from absolute path?

爱⌒轻易说出口 提交于 2019-11-26 15:19:56
问题 If there any API could retrieve file name from an absolute file path? e.g. "foo.txt" from "/var/www/foo.txt" I know it works with string operation, like fullpath.replace(/.+\//, '') but I want to know is there a more 'formal' way, like file.getName() in java, could do it. NodeJS get file name from absolute path? 回答1: Use the basename method of the path module: path.basename('/foo/bar/baz/asdf/quux.html') // returns 'quux.html' Here is the documentation the above example is taken from. 回答2: To

How to download a file with Node.js (without using third-party libraries)?

纵饮孤独 提交于 2019-11-26 01:24:37
问题 How do I download a file with Node.js without using third-party libraries ? I don\'t need anything special. I only want to download a file from a given URL, and then save it to a given directory. 回答1: You can create an HTTP GET request and pipe its response into a writable file stream: const http = require('http'); const fs = require('fs'); const file = fs.createWriteStream("file.jpg"); const request = http.get("http://i3.ytimg.com/vi/J---aiyznGQ/mqdefault.jpg", function(response) { response

Writing files in Node.js

删除回忆录丶 提交于 2019-11-25 21:51:47
问题 I\'ve been trying to find a way to write to a file when using Node.js, but with no success. How can I do that? 回答1: There are a lot of details in the File System API. The most common way is: const fs = require('fs'); fs.writeFile("/tmp/test", "Hey there!", function(err) { if(err) { return console.log(err); } console.log("The file was saved!"); }); 回答2: Currently there are three ways to write a file: fs.write(fd, buffer, offset, length, position, callback) You need to wait for the callback to