Skip to content

Commit be299ae

Browse files
author
haoxin
committedJun 30, 2017
upgrade to koa-send@4
1 parent a94f34c commit be299ae

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed
 

‎index.js

+21-15
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
const debug = require('debug')('koa-static')
9-
const resolve = require('path').resolve
9+
const { resolve } = require('path')
1010
const assert = require('assert')
1111
const send = require('koa-send')
1212

@@ -36,25 +36,31 @@ function serve (root, opts) {
3636
if (opts.index !== false) opts.index = opts.index || 'index.html'
3737

3838
if (!opts.defer) {
39-
return function serve (ctx, next) {
39+
return async function serve (ctx, next) {
4040
if (ctx.method === 'HEAD' || ctx.method === 'GET') {
41-
return send(ctx, ctx.path, opts).then(done => {
42-
if (!done) {
43-
return next()
44-
}
45-
})
41+
try {
42+
await send(ctx, ctx.path, opts)
43+
} catch (err) {
44+
await next()
45+
}
46+
return
4647
}
47-
return next()
48+
49+
await next()
4850
}
4951
}
5052

51-
return function serve (ctx, next) {
52-
return next().then(() => {
53-
if (ctx.method !== 'HEAD' && ctx.method !== 'GET') return
54-
// response is already handled
55-
if (ctx.body != null || ctx.status !== 404) return // eslint-disable-line
53+
return async function serve (ctx, next) {
54+
await next()
55+
56+
if (ctx.method !== 'HEAD' && ctx.method !== 'GET') return
57+
// response is already handled
58+
if (ctx.body != null || ctx.status !== 404) return // eslint-disable-line
5659

57-
return send(ctx, ctx.path, opts)
58-
})
60+
try {
61+
await send(ctx, ctx.path, opts)
62+
} catch (err) {
63+
ctx.status = err.status || 500
64+
}
5965
}
6066
}

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"license": "MIT",
2929
"dependencies": {
3030
"debug": "^2.6.8",
31-
"koa-send": "^3.2.0"
31+
"koa-send": "^4.1.0"
3232
},
3333
"scripts": {
3434
"lint": "eslint --fix .",

0 commit comments

Comments
 (0)
Please sign in to comment.