同步捕获
异步捕获
异常抛出
代码:https://github.com/fengchunjian/nodejs_examples/tree/master/routerv6
//models/Exception.js
module.exports = {
expfun : function(flag) {
if (flag == 0) {
throw "我是例外";
}
return "success";
}
}
//models/optfile.js
var fs = require('fs')
module.exports = {
readImg : function(path, res) {
fs.readFile(path, "binary", function(err, file) {
if (err) {
console.log(err);
return;
} else {
console.log("异步读取图片完成");
res.write(file, "binary");
res.end();
}
});
},
writefile : function(path, data, recall) {
fs.writeFile(path, data, function(err) {
if (err) {
throw err;
}
console.log("异步写文件完成");
recall("异步写文件完成");
});
},
readfile : function(path, recall) {
fs.readFile(path, function(err, data) {
if (err) {
console.log("异步捕获异常:"+err);
recall("异步捕获异常:"+err.toString());
} else {
recall(data);
}
});
console.log("异步读文件完成");
},
}
//models/router.js
var optfile = require("./optfile");
var exception = require("./Exception");
function getRecall(req, res) {
res.writeHead(200, {'Content-Type':'text/html;charset=utf-8'});
function recall(data) {
res.write(data);
res.end();
}
return recall;
}
module.exports = {
testexp : function(req, res) {
recall = getRecall(req, res);
try {
data = exception.expfun(0);
recall(data);
} catch(err) {
console.log("捕获自定义异常:"+err);
recall("捕获自定义异常:" + err.toString());
}
},
readImg : function(req, res) {
res.writeHead(200, {'Content-Type':'image/jpeg'});
optfile.readImg("./imgs/nodejs.jpg", res);
},
writefile : function(req, res) {
recall = getRecall(req, res);
optfile.writefile("./file.txt", "异步文件写入", recall);
},
login : function(req, res) {
recall = getRecall(req, res);
optfile.readfile("./views/loginx.html", recall);
},
zhuce : function(req, res) {
recall = getRecall(req, res);
optfile.readfile("./views/zhuce.html", recall);
}
}
//routercall.js
var http = require('http');
var url = require('url');
var router = require('./models/router');
http.createServer(function (request, response) {
if(request.url!=="/favicon.ico") {
var pathname = url.parse(request.url).pathname;
pathname = pathname.replace(/\//, '');
try {
router[pathname](request, response);
} catch(err) {
console.log("同步捕获异常:"+err);
response.writeHead(200,{'Content-Type':'text/html;charset=utf-8'});
response.write("同步捕获异常:"+err.toString());
response.end();
}
console.log("主程序执行完毕");
}
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');
node routercall.js
Server running at http://127.0.0.1:8000/
异步读文件完成
主程序执行完毕
异步捕获异常:Error: ENOENT: no such file or directory, open './views/loginx.html'
异步读文件完成
主程序执行完毕
同步捕获异常:TypeError: router[pathname] is not a function
主程序执行完毕
捕获自定义异常:我是例外
主程序执行完毕
curl http://localhost:8000/login
异步捕获异常:Error: ENOENT: no such file or directory, open './views/loginx.html'
curl http://localhost:8000/zhuce
注册界面
curl http://localhost:8000/xxx
同步捕获异常:TypeError: router[pathname] is not a function
curl http://localhost:8000/testexp
捕获自定义异常:我是例外
参考文档
nodejs9_异常处理(n9_exception)
http://www.yuankuwang.com/web/index.php?r=respool/resview&rpid=42
node.js教程9_异常处理
http://edu.51cto.com/center/course/lesson/index?id=124533
网友评论