-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
52 lines (42 loc) · 1.33 KB
/
app.js
File metadata and controls
52 lines (42 loc) · 1.33 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const Koa = require('koa2')
const Router = require('koa-router')
const cors = require('koa2-cors')
const static = require('koa-static')
const bodyParser = require('koa-bodyparser')
const path = require('path')
const { host, port } = require('./utils/path')
const manage = require('./router/manage')
const web = require('./router/web')
const nomatch = require('./router/nomatch')
const app = new Koa()
const router = new Router()
router.get('/', async (ctx) => {
ctx.body = {
index: 'index',
}
})
router.use('/manage', manage.routes(), manage.allowedMethods())
router.use('/web', web.routes(), web.allowedMethods())
router.use('/404', nomatch.routes(), nomatch.allowedMethods())
// 重定向404
app.use(async (ctx, next) => {
// 先执行下一个中间件
await next()
if (parseInt(ctx.status) === 404) {
ctx.response.redirect('/404')
}
})
// 后端允许跨域
app.use(cors())
// 接收参数
app.use(bodyParser())
// 路由重定向
// router.redirect('/', '/manage')
app.use(router.routes(), router.allowedMethods())
// 读取静态资源 须在路由后面
app.use(static(path.join(__dirname, 'static')))
app.use(static(path.join(__dirname, 'router/manage/upload')))
// 启动端口监听
app.listen(port, () => {
console.log(`Server is running at ${host}:${port}`)
})