Tag: fs

使用可写入stream写入文件时Node.js EBADF错误

我试图使用Node.js来处理一个500MB的Apache日志文件,从中转换它的语法 ip.ip.ip.ip – – [02/Aug/2012:05:01:17 -0600] "GET /path/of/access/ HTTP/1.1" 302 26 至 ip.ip.ip.ip – – 02/Aug/2012:05:01:17 GET /path/of/access/ HTTP/1.1 302 26 ,然后写入另一个文本文件。 为了更好的内存控制和性能,我使用了fs.createReadStream和fs.createWriteStream ,但是只能把第一行写入output.txt ,因为脚本以错误结束: { [Error: EBADF, write] errno: 9, code: 'EBADF' } 在这里我发布了一些可能有助于debugging的信息。 input.txt头像: ip.ip.ip.ip – – [02/Aug/2012:05:01:17 -0600] "GET /path/of/access/ HTTP/1.1" 302 26 ip.ip.ip.ip – – [02/Aug/2012:05:01:17 -0600] "GET /path/of/access/ HTTP/1.1" 302 […]

使用选项“a”的Node.js fs.open生成EACCES错误

我正在尝试在POST请求上使用node.js的fs模块创buildxml文件。 filePath = path.normalize(path.join(profilesDirPath, name + xmlExt)); fs.exists(filePath, function(exists) { if (exists) { callback({ code: h.httpStatus.CONFLICT, resp: 'audio profile group with such name exists' }); return; } fs.open(filePath, 'a', function(error) { if (error) { callback({ code: h.httpStatus.INTERNAL_SERVER_ERROR, resp: error }); return; } callback(null); }); }); 这会导致错误: { "errno": 3, "code": "EACCES", "path": "/mount/fs/folder/newGroup.xml" } 节点进程运行在Linux和文件夹,我尝试创build这样的文件在同一台机器上。 […]

如何与其他asynchronous模块一起使用asynchronous?

这是一个没有asynchronous的工作testing程序: var fs = require('fs'); function test() { var finalResponse = '', response = ''; function showFinalResponse() { console.log(finalResponse); } function processTheFile(err, data) { if (err) { finalResponse = 'Could not read the file'; } else { response += data; response += '</body></html>'; finalResponse = response; } showFinalResponse(); } function readTheFile(exists) { if (!exists) { […]

使用Nodejs(fs)来访问inputtypes为file的文件

我无法读取不同位置的文件(Node项目目录除外)。 我需要读取一个私钥文件(可能位于文件系统的任何位置),并通过sftp传输一些yaml / yml文件(也可能位于任何位置)。 这些文件在一个文件input字段中select,该字段通过我的index.js(在Node中)的post方法访问。 问题是当我试图读取文件时,我得到一个文件不存在的错误,特别是因为目录默认为Node项目目录。 例如: d:/path/到/节点/项目 d:/这/是/在/的/ keyis 它会尝试阅读这个文件: d:/path/到/节点/项目/ keyis 我不知道为什么我只能得到文件名。 我知道接收整个path是一个安全风险,但我需要的path,即使它不是在任何阶段打印。 这是html表单的代码: <form id="file-upload" name="uploadform" method="post" action="/upload" onsubmit="update()"> <input type='text' id='username' class="btn btn-lg btn-default" name='username' placeholder='Enter Username' style="color:#000"><br> <h3>Select Key:</h3><input type='file' id='key-select' name='keySelect'><br> <h3>Select Yaml:</h3><input type="file" id="file-select" name="yamlfiles[]" multiple accept=".yml, .yaml"/><br> <button type="submit" class="btn btn-lg btn-default" id="upload">Upload Data</button> </form> 然后在index.js中: router.post('/upload', […]

nodejs中的jQuery.when()相当于什么?

我问jquery是什么时候在angular度 ,现在我想在节点做类似的事情。 我需要这样的东西: when(fs.readFile('file1'), fs.readFile('file2')) .done(function( a1, a2 ) { … // do stuff }); 我怎样才能做到这一点? 谢谢。

在节点脚本中,如何监视文件的更改并获取更新的内容?

我正在编写一个脚本来监视一个文件。 只要文件发生变化,它就需要打印到附加的内容到控制台。 我涉及到fs.createReadStream但它似乎没有发送数据通过stream附加到文件。 一般来说,我对stream也是一个新鲜的东西。 希望我只需要有人指点我在正确的方向:)

节点js在复制之前检查文件是否打开

在复制到另一个位置之前,我需要检查一个文件是否打开。下面的代码告诉它是否打开了一个错误代码= EBUSY。 但是,如果文件没有打开,则会擦除文件的内容。 有没有更好的方式来实现这一信息。 fs.open('my-file.dwg','w', function(err,data) { });

无法使用量angular器中的nodejs删除文件

我试图用nodejs fs删除文件,我注意到文件已经生成,然后试图删除(无法删除文件),而文件甚至没有上传在浏览器与量angular器。 生成和删除文件function是使用nodejs fs创build的。 那么我怎样才能把它们放在一起然后等待file upload然后删除文件呢? helper.generateFile(filePath); helper.uploadFile(UploadButtonElement, filePath); uploadButtonElm.click(); helper.deleteFile(filePath); 有没有办法执行deleteFile只有当两个动作完成。 helper.uploadFile(UploadButtonElement, filePath); uploadButtonElm.click(); 谢谢。

nodejs fs模式操作在windows只读属性中

我是NodeJS的新手,我很难解密模式值,并找出如何改变模式来删除像只读属性。 这里是我如何find这个文件夹有一个16822的模式。我如何确定什么16822意味着在Windows中,然后如何更改模式,使它不具有只读ATTR? fs.mkdir('./build'); fs.stat('./build', function(err, stats){ if(stats.isDirectory()){ console.log('Its a dir'); for(var i in stats){ if('function' !== typeof stats[i]){ console.log(i + '\t= ' + stats[i]); } } } });

Node.js fs.writeFile()清空文件

我有一个更新的方法,每16-40毫秒被调用,里面我有这样的代码: this.fs.writeFile("./data.json", JSON.stringify({ totalPlayersOnline: this.totalPlayersOnline, previousDay: this.previousDay, gamesToday: this.gamesToday }), function (err) { if (err) { return console.log(err); } }); 如果服务器抛出一个错误,那么“data.json”文件有时变空。 我如何防止?