734240
目前nodejs常规的异常处理有三种
- try catch
- uncaughtException
- domain 我将cluster和domain进行了结合,用于处理express框架搭建的web应用的异常处理 使用方法:
let express = require("express");
let app = express();
let ecd = require("express-cluster-domain");
app.use(ecd.clusterDomain({
killTimeout:1000,
error:function(err){
console.log("sorry!request bad!");
}
}));
app.get("/",(req,res)=>{
setTimeout(function(){
if(Math.random()>0.5){
throw new Error("Error from timeout");
}else{
console.log("訪問正常");
res.send("request success!")
}
},2000)
});
ecd.startServer(function(){
app.listen(8080);
})
模块github地址https://github.com/839305939wang/express-cluster-domain.git