javascript - Why my fs.readFileSync does not work - Stack Overflow

Why does this code not work?If I mentfs.readFileSync('file.html');the code works and it cre

Why does this code not work? If I ment fs.readFileSync('file.html'); the code works and it creates the file 'file.html' But if I unment it the fs.writeFileSync does not work, and the program crashes with the error:

fs.js:427 return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode); ^ Error: ENOENT, no such file or directory 'file.html' at Object.fs.openSync (fs.js:427:18) at Object.fs.readFileSync (fs.js:284:15) at Object. (/home/pedro/startupEngineering/hw3/Bitstarter/testrestler.js:15:6) at Module._pile (module.js:456:26) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Function.Module.runMain (module.js:497:10) at startup (node.js:119:16) at node.js:901:3

#!/usr/bin/env node


var fs = require('fs');
var rest = require('restler');

var restlerHtmlFile = function(Url) {
  rest.get(Url).on('plete', function(result) {
   fs.writeFileSync('file.html',result);
  });
};

if(require.main == module) {
  restlerHtmlFile('/');
  fs.readFileSync('file.html');
}

else {
 exports.checkHtmlFile = checkHtmlFile;
}

Why does this code not work? If I ment fs.readFileSync('file.html'); the code works and it creates the file 'file.html' But if I unment it the fs.writeFileSync does not work, and the program crashes with the error:

fs.js:427 return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode); ^ Error: ENOENT, no such file or directory 'file.html' at Object.fs.openSync (fs.js:427:18) at Object.fs.readFileSync (fs.js:284:15) at Object. (/home/pedro/startupEngineering/hw3/Bitstarter/testrestler.js:15:6) at Module._pile (module.js:456:26) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Function.Module.runMain (module.js:497:10) at startup (node.js:119:16) at node.js:901:3

#!/usr/bin/env node


var fs = require('fs');
var rest = require('restler');

var restlerHtmlFile = function(Url) {
  rest.get(Url).on('plete', function(result) {
   fs.writeFileSync('file.html',result);
  });
};

if(require.main == module) {
  restlerHtmlFile('http://obscure-refuge-7370.herokuapp./');
  fs.readFileSync('file.html');
}

else {
 exports.checkHtmlFile = checkHtmlFile;
}
Share Improve this question asked Sep 29, 2013 at 17:48 PedroPedro 431 gold badge1 silver badge5 bronze badges 1
  • 1 no such file or directory 'file.html' – SLaks Commented Sep 29, 2013 at 17:49
Add a ment  | 

2 Answers 2

Reset to default 2

Change

var restlerHtmlFile = function(Url) {
  rest.get(Url).on('plete', function(result) {
   fs.writeFileSync('file.html',result);
  });
};

if(require.main == module) {
  restlerHtmlFile('http://obscure-refuge-7370.herokuapp./');
  fs.readFileSync('file.html');
}

to

var restlerHtmlFile = function(Url) {
  rest.get(Url).on('plete', function(result) {
   fs.writeFileSync('file.html',result);
   fs.readFileSync('file.html');
  });
};

if(require.main == module) {
  restlerHtmlFile('http://obscure-refuge-7370.herokuapp./');
}

Second parameter to rest.get(Url).on is an asynchronous call back function, which will be called when plete occurs and only then the file gets created. But you are reading the file, even before the plete occurs. Thats why you are getting this error.

You don't write the file until the plete event fires, but you try to read from it immediately.

Since it doesn't yet exist, you get an exception, which you don't catch, so the program exits before the plete event fires and the file is written.

You need to move the code that tries to read from the file inside the event handler that writes the file.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742415636a4439686.html

相关推荐

  • javascript - Why my fs.readFileSync does not work - Stack Overflow

    Why does this code not work?If I mentfs.readFileSync('file.html');the code works and it cre

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信