1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | const fs = require('fs')
|
7 | const path = require('path')
|
8 | const logger = require('./../logger/koa-logger')('fileMiddle')
|
9 | module.exports = async function(ctx, next) {
|
10 | const proxy = Object.keys(require(path.join(process.cwd(), 'app.config')).proxy) || []
|
11 | if (proxy.findIndex(val => ctx.url.startsWith(val)) > -1) {
|
12 | await next()
|
13 | } else {
|
14 | const res = ctx.res
|
15 |
|
16 | let context = 'File does not exist, please insist that the path is correct'
|
17 | try {
|
18 | logger.info('Read File Request Success', `path ----> ${ctx.url}`)
|
19 | context = fs.readFileSync(path.join(process.cwd(), ctx.url), 'utf-8')
|
20 | res.statusCode = 200
|
21 | } catch (err) {
|
22 | logger.info(context, ctx.url)
|
23 | res.statusCode = 404
|
24 | }
|
25 | res.end(context)
|
26 | }
|
27 | }
|