-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
120 lines (110 loc) · 3.62 KB
/
app.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* @Author: your name
* @Date: 2020-10-09 11:26:39
* @LastEditTime: Sat Apr 10 2021 13:51:40
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: \blueSpace_server\app.js
*/
const Koa = require('koa')
const app = new Koa()
const parameter = require('koa-parameter');
const views = require('koa-views')
const json = require('koa-json')
const onerror = require('koa-onerror')
const bodyparser = require('koa-bodyparser')
const logger = require('koa-logger')
const conditional = require('./middleware/conditionalParameters');
const users = require('./routes/users');
const home = require('./routes/home');
const upload = require('./routes/upload');
const article = require('./routes/article');
const comment = require('./routes/comment');
const search = require('./routes/search');
const stylist = require('./routes/stylist');
const message = require('./routes/message')
const jwtKoa = require('koa-jwt');
const {secret} = require('./bin/config');
const checkToken = require('./middleware/checkToken');
const db = require('./db/dbConnect')
const path = require('path')
const koaBody = require('koa-body')
const cors = require('koa2-cors');
const token = require('./routes/token')
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
console.log('db success');
});
// error handler
onerror(app)
app.use(
cors({
origin: function(ctx) { //设置允许来自指定域名请求
if (ctx.url === '/test') {
return '*'; // 允许来自所有域名请求
}
return 'http://localhost:8080';
},
maxAge: 5, //指定本次预检请求的有效期,单位为秒。
credentials: true, //是否允许发送Cookie
allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'], //设置所允许的HTTP请求方法
allowHeaders: ['Content-Type', 'Authorization', 'Accept'], //设置服务器支持的所有头信息字段
exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'] //设置获取其他自定义字段
})
)
// middlewares
// app.use(bodyparser({
// enableTypes:['json', 'form', 'text']
// }))
app.use(json())
app.use(logger())
app.use(require('koa-static')(__dirname + '/public'))
app.use(views(__dirname + '/views', {
extension: 'ejs'
}))
app.use(jwtKoa({
secret: secret
}).unless({
path: [/^\/api\/users\/login/,/^\/api\/users\/register/,/^\/api\/token\/*/,/^\/api\/admin\/login/]
}));
app.use(checkToken());
app.use(koaBody({
multipart: true,
strict: false,
formidable: {
// uploadDir:path.join(__dirname,'public/upload/'),
keepExtensions: true,
// onFileBegin:(name,file) => {
// const dir = path.join(__dirname,`public/upload/`)
// if(name === 'publish') {
// file.path = `${dir}/publish/${file.name}`
// } else if (name === '') {
// }
// }
multipart: true
}
}))
// logger
app.use(async (ctx, next) => {
const start = new Date()
await next()
const ms = new Date() - start;
console.log(`${ctx.method} ${ctx.url} - ${ms}ms`)
})
app.use(users.routes(), users.allowedMethods())
app.use(home.routes(), home.allowedMethods())
app.use(upload.routes())
app.use(article.routes(), article.allowedMethods())
app.use(comment.routes(), comment.allowedMethods())
app.use(search.routes(), search.allowedMethods())
app.use(stylist.routes(), stylist.allowedMethods())
app.use(message.routes(), message.allowedMethods())
// 检查token路由
app.use(token.routes(), token.allowedMethods())
// error-handling
app.on('error', (err, ctx) => {
console.error('server error', err, ctx)
});
app.use(conditional(app));
app.use(parameter(app))
module.exports = app