try {
process.nextTick(function () {
foo.bar();
});
} catch (err) {
//can not catch it
}
//express风格的路由
app.get('/index', function (req, res) {
try {
//业务逻辑
} catch (err) {
logger.error(err);
res.statusCode = 500;
return res.json({success: false, message: '服务器异常'});
}
});
app.get('/index', function (req, res) {
// 业务逻辑
});
process.on('uncaughtException', function (err) {
logger.error(err);
});
var domain = require('domain');
//引入一个domain的中间件,将每一个请求都包裹在一个独立的domain中
//domain来处理异常
app.use(function (req,res, next) {
var d = domain.create();
//监听domain的错误事件
d.on('error', function (err) {
logger.error(err);
res.statusCode = 500;
res.json({sucess:false, messag: '服务器异常'});
d.dispose();
});
d.add(req);
d.add(res);
d.run(next);
});
app.get('/index', function (req, res) {
//处理业务
});
var http = require('http');
var connect = require('connect');
var RedisStore = require('connect-redis')(connect);
var domainMiddleware = require('domain-middleware');
var server = http.createServer();
var app = connect();
app.use(connect.session({
key: 'key',
secret: 'secret',
store: new RedisStore(6379, 'localhost')
}));
//domainMiddleware的使用可以看前面的链接
app.use(domainMiddleware({
server: server,
killTimeout: 30000
}));
var domain = require('domain');
var redis = require('redis');
var cache = redis.createClient(6379, 'localhost');
function error() {
cache.get('a', function () {
throw new Error('something wrong');
});
}
function ok () {
setTimeout(function () {
throw new Error('something wrong');
}, 100);
}
var d = domain.create();
d.on('error', function (err) {
console.log(err);
});
d.run(ok); //domain捕获到异常
d.run(error); //异常被抛出
function laterCall() {
console.log('print me later');
}
process.nextTick(laterCallback);
console.log('print me first');
//简化后的domain传递部分代码
function nextDomainTick(callback) {
nextTickQueue.push({callback: callback, domain: process.domain});
}
function _tickDomainCallback() {
var tock = nextTickQueue.pop();
//设置process.domain = tock.domain
tock.domain && tock.domain.enter();
callback();
//清除process.domain
tock.domain && tock.domain.exit();
}
};
if (exports.usingDomains) {
// if there is an active domain, then attach to it.
domain = domain || require('domain');
if (domain.active && !(this instanceof domain.Domain)) {
this.domain = domain.active;
}
}
if (process.domain) timer.domain = process.domain;
// create a top-level domain for the server
var serverDomain = domain.create();
serverDomain.run(function() {
// server is created in the scope of serverDomain
http.createServer(function(req, res) {
// req and res are also created in the scope of serverDomain
// however, we'd prefer to have a separate domain for each request.
// create it first thing, and add req and res to it.
var reqd = domain.create();
reqd.add(req);
reqd.add(res);
reqd.on('error', function(er) {
console.error('Error', er, req.url);
try {
res.writeHead(500);
res.end('Error occurred, sorry.');
} catch (er) {
console.error('Error sending 500', er, req.url);
}
});
}).listen(1337);
});
var domain = require('domain');
var EventEmitter = require('events').EventEmitter;
var e = new EventEmitter();
var timer = setTimeout(function () {
e.emit('data');
}, 10);
function next() {
e.once('data', function () {
throw new Error('something wrong here');
});
}
var d = domain.create();
d.on('error', function () {
console.log('cache by domain');
});
d.run(next);
d.add(timer); //or d.add(e);
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有