-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
56 lines (50 loc) · 1.76 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
// 项目入口文件
const Koa = require('koa');
const path = require('path');
const static = require('koa-static')
const bodyParser = require('koa-bodyparser')
const koaLogger = require('koa-logger')
const Moment = require("moment")
const cors = require('koa2-cors')
const router = require('koa-router')() //引入路由模块
const helmet = require("koa-helmet") //安全问题
const { loggerMiddleware,logger } = require('./middlewares/logger')
const { errorHandler, responseHandler } = require('./middlewares/response')
const publicRouter = require('./routers/public/index')
const privateRouter = require('./routers/private/index')
const koaBady = require('koa-body');
// // 使用日志中间件
// const logger = koaLogger((str) => {
// console.log(Moment().format('YYYY-MM-DD HH:MM:SS')+str);
// });
// 配置控制台日志中间件
// app.use(logger)
const app = new Koa();
app.use(static(path.join(__dirname,'public')));
app.use(koaBady({
multipart:true, //支持文件上传
strict:false,//设为false strict 参数:如果启用,则不解析GET,HEAD,DELETE请求,默认为true
formidable:{
maxFieldsSize:5*1024*1024, //最大为5
multipart:true,
}
}))
app.use(loggerMiddleware)
app.use(errorHandler)
// app.use(bodyParser())
app.use(cors())
app.use(helmet())
publicRouter(app)
privateRouter(app)
// 中间件response
app.use(responseHandler)
//初始化路由组件
// 配置静态资源服务 需在router后面,否则会冲突
// 静态资源目录对于相对入口文件index.js的路径
// const staticPath = './public';
// app.use(static(
// path.join( __dirname, staticPath)
// ))
app.listen(4002,() => {
console.log('【node-server】 is listen in 4002');
});