简体   繁体   English

我想使用节点js返回文件的内容

[英]I want to return the contents of file using node js

I have written the following code in protractor. 我用量角器编写了以下代码。

helper.js: helper.js:

var fs = require('fs');
helper = function(){
    this.blnReturn = function(){
        var filePath = '../Protractor_PgObjModel/Results/DontDelete.txt';
        fs.readFileSync(filePath, {encoding: 'utf-8'}, function (err, data){
            if (!err) {
                console.log(data);
                return data;
            } else {
               return "False";
            }
        });
    };
};
module.exports = new helper();

------------actual file where the above js is being called------------------- ------------上面的js被调用的实际文件-------------------

describe("read contents of file",function(){
  var helper = require("../GenericUtilities/helper.js");
  it('To Test read data',function(){
    console.log("helper test - " + helper.blnReturn());   
  });
});

-------Output------------- -------输出-------------

helper test - undefined

Any help in this regard is much appreciated as it is blocking my work. 非常感谢这方面的帮助,因为它阻碍了我的工作。

you are confusing between reading file synchronously( readFileSync ) and async( readFile ). 您在同步读取文件( readFileSync )和异步( readFile )之间感到困惑。

you are trying to read the file synchronously, but also using a callback parameter, the right way would be either 您正在尝试同步读取文件,但也使用回调参数,正确的方法是

return fs.readFileSync(filePath, {encoding: 'utf-8'});

or 要么

this.blnReturn = function(cb){
    ...
    fs.readFileSync(filePath, {encoding: 'utf-8'}, function (err, data){
        if (!err) {
            console.log(data);
            cb(data);
        } else {
           cb("False");
        }
    });

also, on an unrelated note, var keyword is missing in helper definition, the helper.js can be reduced to: 另外,不相关的注释是, helper定义中缺少var关键字,可以将helper.js简化为:

var fs = require('fs');
function helper(){}

helper.prototype.blnReturn = function(){
    var filePath = '../request.js';
    return fs.readFileSync(filePath, {encoding: 'utf-8'});
};

module.exports = new helper();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想使用node.js上传文件 - I Want to Upload a File Using node.js 当文件内容更改时,使用node.js将更新的内容发送给用户 - When file contents change, send updated contents to user using node.js How to import excel file using HTML Input file and read the file content in Node.js (How to send the fullpath to Node.js) - How to import excel file using HTML Input file and read the file contents in Node.js (How to send the fullpath to Node.js) 我想在输入框、表格或使用 node.js 的 div 文件中显示存储在 mysql 数据库中的数据。 - I want to display data stored in mysql database in .html file on input boxes,tables,or in a div using node.js 我想从HTML文档中获取文本框值,并在节点JS中使用Json文件按下提交按钮时发布它们 - I want to get my textbox values from HTML document and post those when submit button is pressed using Json file in node JS 如何使用 node.js 读取输入文件、修改其内容并将结果保存到输出文件? - How do I read an input file, modify its contents and save the result to an output file with node.js? 使用node.js将文本文件中的正则表达式替换为文件内容 - Replace regular expression in text file with file contents using node.js 如何使用JSZip使用node.js中的缓冲区内容生成zip文件? - How to generate zip file using Buffer contents in node.js using JSZip? 我是 node js 的新手,我想从 html 文件路由一个页面,但它不起作用--- Node js 路由问题 - I am new to node js , I want to route a page from html file but its not working--- Node js routing problem Node.js在本地返回JS文件 - Node.js return a JS file locally
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM