Skip to content

Commit

Permalink
Merge pull request DesertsP#16 from sdjcw/master
Browse files Browse the repository at this point in the history
fix: 更新全局处理器;增加默认超时
  • Loading branch information
sdjcw committed Jun 7, 2016
2 parents c7c7473 + c78325d commit fb9ea87
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 42 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
node_modules
start.sh
.avoscloud

# VIM
*~
Expand Down
62 changes: 20 additions & 42 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';
var domain = require('domain');
var express = require('express');
var timeout = require('connect-timeout');
var path = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
Expand All @@ -14,6 +14,9 @@ app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.static('public'));

// 设置默认超时时间
app.use(timeout('15s'));

// 加载云函数定义
require('./cloud');
// 加载云引擎中间件
Expand All @@ -23,27 +26,6 @@ app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());

// 未处理异常捕获 middleware
app.use(function(req, res, next) {
var d = null;
if (process.domain) {
d = process.domain;
} else {
d = domain.create();
}
d.add(req);
d.add(res);
d.on('error', function(err) {
console.error('uncaughtException url=%s, msg=%s', req.url, err.stack || err.message || err);
if(!res.finished) {
res.statusCode = 500;
res.setHeader('content-type', 'application/json; charset=UTF-8');
res.end('uncaughtException');
}
});
d.run(next);
});

app.get('/', function(req, res) {
res.render('index', { currentTime: new Date() });
});
Expand All @@ -61,28 +43,24 @@ app.use(function(req, res, next) {
});

// error handlers

// 如果是开发环境,则将异常堆栈输出到页面,方便开发调试
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) { // jshint ignore:line
var statusCode = err.status || 500;
if(statusCode === 500) {
console.error(err.stack || err);
}
res.status(statusCode);
res.render('error', {
message: err.message || err,
error: err
});
});
}

// 如果是非开发环境,则页面只输出简单的错误信息
app.use(function(err, req, res, next) { // jshint ignore:line
res.status(err.status || 500);
var statusCode = err.status || 500;
if(statusCode === 500) {
console.error(err.stack || err);
}
if(req.timedout) {
console.error('请求超时: url=%s, timeout=%d, 请确认方法执行耗时很长,或没有正确的 response 回调。', req.originalUrl, err.timeout);
}
res.status(statusCode);
// 默认不输出异常详情
var error = {}
if (app.get('env') === 'development') {
// 如果是开发环境,则将异常堆栈输出到页面,方便开发调试
error = err;
}
res.render('error', {
message: err.message || err,
error: {}
message: err.message,
error: error
});
});

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"license": "MIT",
"dependencies": {
"body-parser": "1.12.3",
"connect-timeout": "^1.7.0",
"cookie-parser": "^1.3.5",
"ejs": "2.3.1",
"express": "4.12.3",
Expand Down
8 changes: 8 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,12 @@ var app = require('./app');
var PORT = parseInt(process.env.LEANCLOUD_APP_PORT || 3000);
app.listen(PORT, function () {
console.log('Node app is running, port:', PORT);

// 注册全局未捕获异常处理器
process.on('uncaughtException', function(err) {
console.error("Caught exception:", err.stack);
});
process.on('unhandledRejection', function(reason, p) {
console.error("Unhandled Rejection at: Promise ", p, " reason: ", reason.stack);
});
});

0 comments on commit fb9ea87

Please sign in to comment.