refactor: 重构优化

This commit is contained in:
xiaojunnuo
2021-02-04 18:44:16 +08:00
parent a39dac4dbd
commit a25a15ca6e
59 changed files with 3903 additions and 967 deletions
+5 -7
View File
@@ -1,5 +1,4 @@
import Koa from 'koa'
import views from 'koa-views'
import json from 'koa-json'
import onerror from 'koa-onerror'
import bodyparser from 'koa-bodyparser'
@@ -7,6 +6,7 @@ import logger from 'koa-logger'
import Static from 'koa-static'
import fs from 'fs'
import _ from 'lodash-es'
const app = new Koa()
// error handler
@@ -21,10 +21,6 @@ app.use(logger())
app.use(Static(new URL('public', import.meta.url).pathname))
app.use(views(new URL('views', import.meta.url).pathname, {
extension: 'pug'
}))
// logger
app.use(async (ctx, next) => {
const start = new Date()
@@ -33,15 +29,17 @@ app.use(async (ctx, next) => {
console.log(`${ctx.method} ${ctx.url} - ${ms}ms`)
})
console.log('url', import.meta.url)
// routes
const files = fs.readdirSync(new URL('controllers', import.meta.url).pathname)
const files = fs.readdirSync(new URL('controllers/', import.meta.url))
// 过滤出.js文件:
const jsFiles = files.filter((f) => {
return f.endsWith('.js')
})
_.forEach(jsFiles, async item => {
let mapping = await import(new URL('controllers/' + item, import.meta.url).pathname)
let mapping = await import(new URL('controllers/' + item, import.meta.url))
mapping = mapping.default
app.use(mapping.routes(), mapping.allowedMethods())
})