1 Star 0 Fork 0

YinXiaobo/koa体验

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
app.js 1.33 KB
一键复制 编辑 原始数据 按行查看 历史
YinXiaobo 提交于 2018-12-07 17:09 . koa2体验
const Koa = require('koa')
const app = new Koa()
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 mongoose = require('mongoose')
const dbConfig = require('./dbs/config')
const session = require('koa-generic-session')
const redis = require('koa-redis')
const index = require('./routes/index')
const users = require('./routes/users')
const pv = require('./middleware/koa-pv')
// error handler
onerror(app)
app.keys = ['keys', 'keyskeys']
// middlewares
app.use(session({
key: 'mt',
prefix: 'mtid',
store: new redis()
}))
app.use(pv())
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'
}))
// 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`)
})
// routes
app.use(index.routes(), index.allowedMethods())
app.use(users.routes(), users.allowedMethods())
mongoose.connect(dbConfig.dbs, {
useNewUrlParser: true
})
// error-handling
app.on('error', (err, ctx) => {
console.error('server error', err, ctx)
});
module.exports = app
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/YinXiaobo/koa_experience.git
git@gitee.com:YinXiaobo/koa_experience.git
YinXiaobo
koa_experience
koa体验
master

搜索帮助