Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
lienren committed Jan 10, 2018
1 parent b58e50b commit 4a93710
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 31 deletions.
17 changes: 15 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: Lienren
* @Date: 2018-01-02 14:28:21
* @Last Modified by: Lienren
* @Last Modified time: 2018-01-09 11:02:51
* @Last Modified time: 2018-01-10 14:41:10
*/
'use strict';
const http = require('http');
Expand All @@ -15,7 +15,7 @@ const app = new koa();

const mysqlconfig = require('./configs/mysql_config');
const sqlhelper = require('mysql-helper-simple')(mysqlconfig);
app.context.db = sqlhelper
app.context.db = sqlhelper;

// 静态存放地址
const staticPath = './static';
Expand All @@ -41,6 +41,19 @@ app.use(
// 使用koa-bodyparser中间件
app.use(bodyParser());

// 全局处理异常
app.use(async (ctx, next) => {
try {
await next();
} catch (err) {
ctx.body = {
code: err.code,
message: err.message
};
ctx.status = err.status || 500;
}
});

// 使用路由
const router = require('./router');
app.use(router);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
"description": "koa demo",
"main": "app.js",
"dependencies": {
"async": "^2.6.0",
"koa": "^2.4.1",
"koa-bodyparser": "^4.2.0",
"koa-router": "^7.3.0",
"koa-static": "^4.0.2",
"koa2-cors": "^2.0.5",
"mysql-helper-simple": "^1.1.1",
"mysql-helper-simple": "^1.1.2",
"squel": "^5.12.0"
},
"devDependencies": {},
Expand Down
38 changes: 13 additions & 25 deletions router.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: Lienren
* @Date: 2018-01-08 16:21:55
* @Last Modified by: Lienren
* @Last Modified time: 2018-01-09 09:57:09
* @Last Modified time: 2018-01-10 17:20:06
*/
'use strict';
const Router = require('koa-router');
Expand All @@ -25,37 +25,25 @@ router
.from('sm_user')
.toParam();

await ctx.db
.query({
sql: sql.text,
values: sql.values
})
.then(result => {
console.log(result);
ctx.body = {
...result
};
});
let result = await ctx.db.query({
sql: sql.text,
values: sql.values
});

ctx.body = {
...result
};
})
.get('/getbatch', async (ctx, next) => {
let sql = squel
.select()
.from('sm_user')
.toParam();

await ctx.db.batch([ctx.db.query({ sql: sql.text, values: sql.values })], {
completeHanding: results => {
console.log('result:', results);
ctx.body = {
...results[0]
};
},
exceptionHandling: err => {
ctx.body = {
...err
};
}
});
let results = await ctx.db.batch([ctx.db.query({ sql: sql.text, values: sql.values })]);
ctx.body = {
...results[0]
};
});

module.exports = router.routes();
16 changes: 13 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ any-promise@^1.0.0, any-promise@^1.1.0:
version "1.3.0"
resolved "http://registry.npm.taobao.org/any-promise/download/any-promise-1.3.0.tgz#abc6afeedcea52e809cdc0376aed3ce39635d17f"

async@^2.6.0:
version "2.6.0"
resolved "http://registry.npm.taobao.org/async/download/async-2.6.0.tgz#61a29abb6fcc026fea77e56d1c6ec53a795951f4"
dependencies:
lodash "^4.14.0"

[email protected]:
version "4.0.4"
resolved "http://registry.npm.taobao.org/bignumber.js/download/bignumber.js-4.0.4.tgz#7c40f5abcd2d6623ab7b99682ee7db81b11889a4"
Expand Down Expand Up @@ -241,6 +247,10 @@ koa@^2.4.1:
type-is "^1.5.5"
vary "^1.0.0"

lodash@^4.14.0:
version "4.17.4"
resolved "http://registry.npm.taobao.org/lodash/download/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"

[email protected]:
version "0.3.0"
resolved "http://registry.npm.taobao.org/media-typer/download/media-typer-0.3.0.tgz#8710d7af0aa626f8fffa1ce00168545263255748"
Expand All @@ -263,9 +273,9 @@ [email protected]:
version "2.0.0"
resolved "http://registry.npm.taobao.org/ms/download/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"

mysql-helper-simple@^1.1.1:
version "1.1.1"
resolved "http://registry.npm.taobao.org/mysql-helper-simple/download/mysql-helper-simple-1.1.1.tgz#5b0f2a4944de196983181110684aaed2829374e9"
mysql-helper-simple@^1.1.2:
version "1.1.2"
resolved "http://registry.npm.taobao.org/mysql-helper-simple/download/mysql-helper-simple-1.1.2.tgz#19fdbdfe5dafa700faed6f894c4c38ff93ec03cc"
dependencies:
mysql "^2.15.0"

Expand Down

0 comments on commit 4a93710

Please sign in to comment.