node.js学习随笔20170817
2017-08-17 14:26
399 查看
一、node.js处理异常的三种方法
1、同步处理异常
二、node.js常用导入包
1、var querystring=require('querystring');
作用:转换为json对象
将异步获取的数据,使用该类转换为json对象。
2、var async=require('async');
作用:串行、并行交互
安装async包指令:npm install async --g
3、var mysql=require('mysql');
作用:引入MySQL的调用
4、var events=require('events');
作用:事件调用
三、链接运行方式
var async=require('async');
1、串行无关联
2、并行无关联
3、串行有关联
备注:此处的setTimeout()和setInterval()所指的对象不再是js中的window,而是node.js的全局对象global下的方法,其地位相当于js中的window。
因此,这里clearInterval(this);在node中可以清除计时器,而在js中这样写则不行,还算是要使用变量名。
1、同步处理异常
try{ 事件处理; }catch(err){ console.log("访问报错如下所示"); console.log(err.toString()); response.write(err.toString()); response.end(); }2、异步处理异常
fs.readFile(path,function(err,data){ if(err){ console.log("访问报错如下所示"); console.log(err.toString()); response.write(err.toString()); response.end(); }else{ recall(data); //闭包 } });3、throw 抛出异常
fs.readFile(path,function(err,data){ if(err){ throw '读取失败'; }else{ recall(data); //闭包 } });
二、node.js常用导入包
1、var querystring=require('querystring');
作用:转换为json对象
将异步获取的数据,使用该类转换为json对象。
2、var async=require('async');
作用:串行、并行交互
安装async包指令:npm install async --g
3、var mysql=require('mysql');
作用:引入MySQL的调用
4、var events=require('events');
作用:事件调用
var eventEmitter=new events.EventEmitter(); emitter.addListener(event, listener)==on //添加监听 emitter.on(event, listener) emitter.once(event, listener)//一次性的监听器 emitter.removeListener(event, listener) //删除指定监听 emitter.removeAllListeners([event]) //删除所有监听 emitter.setMaxListeners(n) //默认情况下当一个事件的监听超过10个时,EventEmitter 将打印警告信息,0表无限制 emitter.listeners(event) //返回特定事件的事件监听器集合 emitter.emit(event, [arg1], [arg2], [...]) //用提供的参数按顺序执行每个事件监听器。
三、链接运行方式
var async=require('async');
1、串行无关联
async.series( { 'ma':function(done){ var i=0; setInterval(function(){ console.log('ma正在运行'+new Date()); i++; if(i==3){ clearInterval(this); done(null,'ma运行完毕'); //这里null表示运行正确,当为其它任何值时,表示出错 } },1000); }, 'mb':function(done){ var i=0; setInterval(function(){ console.log('mb正在运行'+new Date()); i++; if(i==3){ clearInterval(this); done(null,'mb运行完毕'); } },1000); } }, function(err,rs){ console.log(err); console.log(rs); } );
2、并行无关联
async.parallel(); [ˈpærəˌlɛl] //同串行无关联。
3、串行有关联
async.waterfall //瀑布流 async.waterfall([ function(done){ var i=0; setInterval(function(){ if(3==i){ clearInterval(this); done(null,'第一步结束:'); return; } console.log('在第一步中:'+new Date()); i++; },1000); }, function(preVal,done){ var i=0; setInterval(function(){ if(3==i){ clearInterval(this); done(null,'第二步结束:'); return; } console.log(preVal+new Date()); i++; },1000); } ], function(err,rs){ console.log(err); console.log(rs); } );
备注:此处的setTimeout()和setInterval()所指的对象不再是js中的window,而是node.js的全局对象global下的方法,其地位相当于js中的window。
因此,这里clearInterval(this);在node中可以清除计时器,而在js中这样写则不行,还算是要使用变量名。
相关文章推荐
- Node.Js 学习随笔2
- node.js学习随笔——20170809
- Node.js 学习随笔
- Node.Js 学习随笔2
- Node.js学习随笔(1)
- Node.js 学习随笔
- node.js学习随笔
- Node.js学习(14)----EJS模板引擎
- Node.js学习-一步步完善一个web框架-1
- node.js学习资料
- Node.js学习笔记(3、http模块)
- Node.js 学习一(Node.js 安装)
- node.js学习系列
- node.js学习之事件模块Events的使用示例
- node.js学习笔记1——node.js是什么
- node学习第一天 -运行js
- 学习5——node.js安装及MongoDB数据库连接
- Node.js环境搭建和学习(windwos环境)
- Node.js学习记录1
- node.js学习(2)--路由功能以及表单上传