-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.js
More file actions
52 lines (38 loc) · 1.38 KB
/
app.js
File metadata and controls
52 lines (38 loc) · 1.38 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
import Koa from 'koa';
import Router from 'koa-router'
import cors from 'koa2-cors'
import bodyParser from 'koa-bodyparser'
import mongoose from 'mongoose'
import jwtKoa from 'koa-jwt'//验证token
import fs from 'fs'
import path from 'path'
import userRoutes from './routes/user.js'
import homeRoutes from './routes/home.js'
import config from './config'
import errorHandle from './middleware/errorHandle'
// import authTokenCheck from './middleware/authTokenCheck'
const app = new Koa();
const secret = 'appIdSessionId' //生成Token 的秘钥
const models = path.join(__dirname, './models');
const router = new Router();
mongoose.Promise = Promise
mongoose.connect(`mongodb://localhost:27017/koa2Wechat`, { useNewUrlParser: true });
// app.use(authTokenCheck);
// 遍历引入mongo
fs.readdirSync(models)
.filter(file => ~file.search(/^[^.].*\.js$/))
.forEach(file => require(path.join(models, file)));
app.use(errorHandle)
.use(cors())
.use(bodyParser())
//全局路由除了path 以外都需要携带token去请求
.use(jwtKoa({secret:secret}).unless({
path: config.app.whiteList
}))
//二级路由 http://localhost:3000/wechatUser
router.use('/user',userRoutes.routes())
router.use('/home',homeRoutes.routes())
app.use(router.routes())
app.use(router.allowedMethods())
// 连接mongoDB
app.listen(config.app.port, () => console.log(`[Server] starting at port ${config.app.port}`))