-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
46 lines (40 loc) · 1.24 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
const Koa = require('koa');
const app = new Koa();
const logger = require('koa-logger');
const path = require('path');
const server = require('koa-static');
const views = require('koa-views');
const bodyParser = require('koa-bodyparser');
const router = require('./routes/main');
const errorHandle = require('./middleware/errorHandle');
const jwt = require('koa-jwt');
const secret = 'albeanfengFFFFFDDDDDBBBBB';
app.use(function(ctx, next){
if (ctx.url.match(/^\/public/)) {
ctx.body = 'unprotected\n';
} else {
return next();
}
});
app.use(jwt({
secret,
}).unless({
path: [/\/api\/register/, /\/api\/login/,/^\/resource/,/\/api\/doRegister/,/\/api\/*/,/\/admin\/*/,]
}));
// 静态资源目录对于相对入口文件index.js的路径
const staticPath = './public';
app.use(server(staticPath));
app.use(bodyParser());
app.use(logger());
// app.use(errorHandle);
// 加载模板引擎
app.use(views(path.join(__dirname, './views'), {
extension: 'ejs'
}));
app.use(router.routes())
.use(router.allowedMethods());
app.on('error', (err, ctx) => {
console.error('server error', err, ctx)
});
app.listen(3000);
console.log('[demo] start-quick is starting at port 3000');