一、异常处理
1、比如输入浏览器的路由的时候,当路由输入一个不存在的路径的时候,这个时候就会直接报错,报错之后server就会崩溃,然后就要手动启动服务,对于一个服务器而言是不能容忍的。这个时候就要用到异常处理。
2、分类
同步异常处理
异步异常处理
二、同步的异常处理
(一)、输入正确的路由
1、n9
var http = require('http');
var url = require('url');
var luyou = require('./luyou');
http.createServer(function (request,response){
if(request.url!=="/favicon.ico"){ //清除第2此访问
pathname=url.parse(request.url).pathname;
pathname = pathname.replace(/\//,'');//替换掉前面的/
try{
luyou[pathname](request,response);
}catch(err){ //回调函数
console.log('aaaaa='+err);
response.writeHead(200,{'Content-Type': 'text/html; charset=utf-8'});
response.write(err.toString());
response.end('');
}
console.log("server执行完毕");
}
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');
2、运行
(二)、输入不正确的路由
三、异步的异常处理
只需要修改两处:
1、假设 luyou
var optfile = require('./models/optfile');
//封装
function getRecall(req ,res ){
res.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});
function recall(data){
res.write(data);
res.end('');//不写则没有http协议尾
}
return recall;
}
module.exports={
//进入
login:function(req,res){
recall =getRecall(req ,res);
optfile.readfile('..../views/login.html',recall); //路径写错
},
//注册
setin:function(req,res){
recall =getRecall(req ,res);
optfile.readfile('./views/setin.html',recall);
},
//写入
writefile:function(req ,res){
recall =greRecall(req ,res);
optfile.writefile('./views/one.text','天气好好!!!',recall);
},
//读图片
showimg:function(req ,res){
res.writeHead(200, {'Content-Type':'image/jpeg'});
optfile.readImg('./imgs/pig.png',res);
}
}
'..../views/login.html',recall); //路径写错
},
//注册
setin:function(req,res){
recall =getRecall(req ,res);
optfile.readfile('./views/setin.html',recall);
},
//写入
writefile:function(req ,res){
recall =greRecall(req ,res);
optfile.writefile('./views/one.text','天气好好!!!',recall);
},
//读图片
showimg:function(req ,res){
res.writeHead(200, {'Content-Type':'image/jpeg'});
optfile.readImg('./imgs/pig.png',res);
}
}
2、optfile
var fs= require('fs');
module.exports={
//读文件
readfileSync:function(path){ //同步读取
//读路径
var data = fs.readFileSync(path,'utf-8');
console.log(data);
console.log("同步方法执行完毕");
},
readfile:function(path,recall){ //异步执行
fs.readFile(path,function(err,data){
//如果错误err
if(err){
console.log("nnnn:"+err);
recall("文件不存在"); //添加
}else{
console.log(data.toString());
recall(data);
}
});
console.log("异步方法执行完毕");
},
//读取二进制图片(传入路径)
readImg:function(path,res){
fs.readFile(path,'binary',function(err,filedata) { //异步执行 'binary' 二进制流的文件
if (err) {
console.log(err);
return;
}else{
res.write(filedata,'binary');
res.end();
}
});
},
//写文件
writefile:function(path,data,recall){ //异步方式
fs.writeFile(path, data, function (err) {
if (err) {
throw err;
}
console.log('It\'s saved!'); //文件被保存
recall('写文件成功!');
});
},
writeFileSync:function(path,data){ //同步方式
fs.writeFileSync(path, data);
console.log("同步写文件完成");
},
}
console.log("nnnn:"+err);
recall("文件不存在"); //添加
}else{
console.log(data.toString());
recall(data);
}
});
console.log("异步方法执行完毕");
},
//读取二进制图片(传入路径)
readImg:function(path,res){
fs.readFile(path,'binary',function(err,filedata) { //异步执行 'binary' 二进制流的文件
if (err) {
console.log(err);
return;
}else{
res.write(filedata,'binary');
res.end();
}
});
},
//写文件
writefile:function(path,data,recall){ //异步方式
fs.writeFile(path, data, function (err) {
if (err) {
throw err;
}
console.log('It\'s saved!'); //文件被保存
recall('写文件成功!');
});
},
writeFileSync:function(path,data){ //同步方式
fs.writeFileSync(path, data);
console.log("同步写文件完成");
},
}
3、运行
============================================================================
四、异常的抛出 -- throw (抛出字符串、对象、数组等)
用try-catch 拿到抛出的异常,作为廉价的消息传递机制 。
1、exception
module.exports={
expfun:function(flag){
if(flag==0){
throw '我是例外';
}
return "success";
}
}
2、n9修改
var http = require('http');
var url = require('url');
var luyou = require('./luyou');
var exception = require('./models/Exception');
http.createServer(function (request,response){
if(request.url!=="/favicon.ico"){ //清除第2此访问
pathname=url.parse(request.url).pathname;
pathname = pathname.replace(/\//,'');//替换掉前面的/
try{
//luyou[pathname](request,response);
data = exception.expfun(0);
response.write(data);
response.end('');
}catch(err){
console.log('aaaaa='+err);
response.writeHead(200,{'Content-Type': 'text/html; charset=utf-8'});
response.write(err.toString());
response.end('');
}
console.log("server执行完毕");
}
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');
var exception = require('./models/Exception');
http.createServer(function (request,response){
if(request.url!=="/favicon.ico"){ //清除第2此访问
pathname=url.parse(request.url).pathname;
pathname = pathname.replace(/\//,'');//替换掉前面的/
try{
//luyou[pathname](request,response);
data = exception.expfun(0);
response.write(data);
response.end('');
}catch(err){
console.log('aaaaa='+err);
response.writeHead(200,{'Content-Type': 'text/html; charset=utf-8'});
response.write(err.toString());
response.end('');
}
console.log("server执行完毕");
}
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');
3、运行
4、或者n9修改为
var http = require('http');
var url = require('url');
var luyou = require('./luyou');
var exception = require('./models/Exception');
http.createServer(function (request,response){
if(request.url!=="/favicon.ico"){ //清除第2此访问
pathname=url.parse(request.url).pathname;
pathname = pathname.replace(/\//,'');//替换掉前面的/
try{
//luyou[pathname](request,response);
data = exception.expfun(10);
response.write(data);
response.end('');
}catch(err){
console.log('aaaaa='+err);
response.writeHead(200,{'Content-Type': 'text/html; charset=utf-8'});
response.write(err.toString());
response.end('');
}
console.log("server执行完毕");
}
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');
var exception = require('./models/Exception');
http.createServer(function (request,response){
if(request.url!=="/favicon.ico"){ //清除第2此访问
pathname=url.parse(request.url).pathname;
pathname = pathname.replace(/\//,'');//替换掉前面的/
try{
//luyou[pathname](request,response);
data = exception.expfun(10);
response.write(data);
response.end('');
}catch(err){
console.log('aaaaa='+err);
response.writeHead(200,{'Content-Type': 'text/html; charset=utf-8'});
response.write(err.toString());
response.end('');
}
console.log("server执行完毕");
}
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');
5、运行
两次结果是不一样的!!