Skip to content

Commit

Permalink
update to v1.1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
jackhutu committed Apr 6, 2016
1 parent 480ddc2 commit 770204c
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 40 deletions.
7 changes: 7 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
1.1.4 / 2016-04-05
==================

* 使用自定义错误中间件替换onerror
* 使用自定义header替换ismobilejs的判断


1.1.3 / 2016-04-05
==================

Expand Down
Empty file removed logs/development-error.log.0
Empty file.
12 changes: 0 additions & 12 deletions logs/test-error.log.0

This file was deleted.

6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jackblog-api-koa",
"version": "1.1.3",
"version": "1.1.4",
"description": "jackblog API koa版",
"main": "server/app.js",
"scripts": {
Expand Down Expand Up @@ -29,7 +29,6 @@
"co": "^4.6.0",
"debug": "^2.2.0",
"ioredis": "^1.15.1",
"ismobilejs": "^0.4.0",
"koa": "^1.2.0",
"koa-bodyparser": "^2.0.1",
"koa-compose": "^2.4.0",
Expand All @@ -40,7 +39,6 @@
"koa-jwt": "^1.1.2",
"koa-logger": "^1.3.0",
"koa-multer": "0.0.2",
"koa-onerror": "^1.3.1",
"koa-passport": "^1.3.0",
"koa-redis": "^2.0.1",
"koa-response-time": "^1.0.2",
Expand All @@ -59,7 +57,7 @@
"gulp": "^3.9.1",
"gulp-coveralls": "^0.1.4",
"gulp-env": "^0.4.0",
"gulp-istanbul": "^0.10.3",
"gulp-istanbul": "^0.10.4",
"gulp-mocha": "^2.2.0",
"gulp-nodemon": "^2.0.6",
"gulp-sequence": "^0.4.5",
Expand Down
19 changes: 10 additions & 9 deletions process.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
{
"apps" : [
{
"name" : "jackblog-api-koa",
"script" : "./server/app.js",
"name" : "jackblog-api-koa",
"script" : "./server/app.js",
"log_date_format" : "YYYY-MM-DD HH:mm Z",
"out_file" : "./logs/pm2-out.log",
"error_file" : "./logs/pm2-err.log",
"pid_file" : "./logs/jackblog-api-koa.pid",
"ignoreWatch" : ["[\\/\\\\]\\./", "node_modules"],
"watch" : "false",
"exec_mode" : "fork_mode", //cluster_mode
"out_file" : "./logs/pm2-out.log",
"error_file" : "./logs/pm2-err.log",
"pid_file" : "./logs/jackblog-api-koa.pid",
"ignoreWatch" : ["[\\/\\\\]\\./", "node_modules"],
"watch" : "false",
"node_args" : "--harmony",
"exec_mode" : "fork_mode", //cluster_mode
"env": {
"NODE_ENV": "production"
"NODE_ENV" : "production"
}
}
]
Expand Down
8 changes: 0 additions & 8 deletions server/api/users/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,6 @@ exports.addUser = function *() {
this.status = 200;
this.body = {success:true,user_id:user._id};
}catch(err){
if(err.errors.nickname){
this.status = 500;
return this.body = {error_msg:err.errors.nickname.message};
}
this.throw(err);
}
}
Expand Down Expand Up @@ -154,10 +150,6 @@ exports.updateUser = function *() {
this.status = 200;
this.body = {success:true,user_id:newUser._id};
}catch(err){
if(err.errors.nickname){
this.status = 500;
return this.body = {error_msg:err.errors.nickname.message};
}
this.throw(err);
}
}
Expand Down
15 changes: 10 additions & 5 deletions server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ const fs = require('fs');
const app = require('koa')();
const mongoose = require('mongoose');
const config = require('./config/env');
const onerror = require('koa-onerror');
const loggerMiddle = require('./util/logs');
const errorHandleMiddle = require('./util/error');

// 连接数据库.
mongoose.connect(config.mongo.uri, config.mongo.options);
const modelsPath = path.join(__dirname, 'model');
Expand All @@ -27,12 +28,16 @@ if(config.seedDB && config.env === 'development') {
//log记录
//router use : this.logger.error('msg')
app.use(loggerMiddle());
onerror(app);
//错误处理中间件
app.use(errorHandleMiddle());
require('./config/koa')(app);
require('./routes')(app);
// app.on('error', function(err, ctx){
// console.error('server error', err);
// });
//错误监听
app.on('error',(err,ctx)=>{
if (process.env.NODE_ENV != 'test') {
console.error('error', err);
}
})
// Start server
app.listen(config.port, function () {
console.log('Koa server listening on %d, in %s mode', config.port, app.env);
Expand Down
3 changes: 1 addition & 2 deletions server/auth/local/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ const mongoose = require('mongoose');
const router = require("koa-router")();
const passport = require('koa-passport');
const User = mongoose.model('User');
const isMobile = require('ismobilejs');
const auth = require('../auth.service');

function checkCaptcha() {
return function *(next) {
//测试环境不用验证码
let error_msg;
if(process.env.NODE_ENV !== 'test' && !isMobile(this.req.headers['user-agent']).any){
if(process.env.NODE_ENV !== 'test' && !this.req.headers.jackblog){
if(!this.request.body.captcha){
error_msg = "验证码不能为空.";
}else if(this.session.captcha !== this.request.body.captcha.toUpperCase()){
Expand Down
25 changes: 25 additions & 0 deletions server/util/error/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

const _ = require('lodash');

function errorHandleMiddle() {
return function *(next) {
try {
yield next;
} catch (err) {
this.status = err.status || 500;
let error_msg = err.message;
if(err.errors && typeof(err.errors) === 'object'){
_.mapValues(err.errors, (item)=>{
if(item.message){
error_msg = item.message
}
});
}
this.body = {error_msg: error_msg};
this.app.emit('error', err, this);
}
}
}

module.exports = errorHandleMiddle;

0 comments on commit 770204c

Please sign in to comment.