node 回调函数

Node.js 回调函数

Node.js 异步编程的直接体现就是回调。

 

nodeJs编辑器中运行代码:  

错误代码:

 var fs = require('fs');

 fs.readFile('input.txt',function(err,data){console.log(err),data.toString()})

 

输出: 

undefined

> { [Error: ENOENT: no such file or directory, open 'C:\Program Files (x86)\nodejs\input.txt']

  errno: -4058,

  code: 'ENOENT',

  syscall: 'open',

  path: 'C:\\Program Files (x86)\\nodejs\\input.txt' }

TypeError: Cannot read property 'toString' of undefined

    at ReadFileContext.callback (repl:1:65)

    at FSReqWrap.readFileAfterOpen [as oncomplete] (fs.js:303:13)

 

错误原因: C:\\Program Files (x86)\\nodejs\\input.txt  这里没有 input.txt 文件

解决方案:  C:\\Program Files (x86)\\nodejs   下新建 input.txt 文件

成功代码:

  var fs = require('fs');

  fs.readFile('input.txt',function(err,data){if(err) return console.log(err);console.log(data.toString())})

undefined

> hello!!!

 

 

 

http://notknow.lofter.com/post/19c190_7c4da9

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值