all files / src/ index.js

100% Statements 16/16
100% Branches 2/2
100% Functions 4/4
100% Lines 13/13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26                          
import { readFile } from 'fs-promise'
import { lookup } from 'mime-types'
 
const staticMiddleware = async (ctx, next) => {
 
    const { response, path } = ctx
 
    const contentType = lookup(path)
    if (!contentType) {
        return await next()
    }
 
    try {
        response.body = await readFile(path.slice(1), 'utf8')
        response.set('Content-Type', contentType)
    } catch (e) {}
 
    await next()
}
 
const staticMiddlewareInit = (config) => (
    async (ctx, next) => await staticMiddleware(ctx, next, config)
)
 
export default staticMiddlewareInit