-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
63 lines (51 loc) · 1.43 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
const Koa = require('koa')
const logger = require('koa-logger')
const helmet = require('koa-helmet')
const favicon = require('koa-favicon')
const cors = require('@koa/cors')
const koaStatic = require('koa-static')
const path = require('path')
const config = require('config')
const swagger = require('swagger-injector')
const mongoose = require('mongoose')
const { uri, options } = config.get('mongoose')
mongoose.connect(uri, options)
const app = new Koa()
// error handling
app.use(require('./middlewares/error-handling')())
// static
app.use(favicon(path.resolve(__dirname, './public/favicon.ico')))
app.use(koaStatic(path.resolve(__dirname, './public')))
// middleware
app.use(helmet())
app.use(cors())
if (app.env === 'development') {
app.use(logger())
}
// 解决swagger-injector返回js 与 koa-helmet 冲突问题
app.use(async (ctx, next) => {
await next();
if (ctx.request.path && ctx.request.path.indexOf('.js') == ctx.request.path.length - 3) {
ctx.set('Content-Type', 'application/javascript');
}
});
// init app
require('./lib/init')(app)
// router
require('./routes')(app)
// swagger
if (app.env === 'development') {
app.use(swagger.koa({
path: `${__dirname}/swagger.json`,
}));
}
// error log
app.on('error', (err, ctx) => {
console.error('server error', err, ctx)
})
if (!module.parent) {
const port = process.env.PORT || '3000'
app.listen(port)
console.log('Listening on ' + port)
}
module.exports = app