Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0830 #25

Open
LeungKaMing opened this issue Aug 31, 2017 · 0 comments
Open

0830 #25

LeungKaMing opened this issue Aug 31, 2017 · 0 comments

Comments

@LeungKaMing
Copy link
Owner

LeungKaMing commented Aug 31, 2017

前言

下面先从服务端部分开始学习。

入口文件 - app.js

const Koa = require('koa')
const app = new Koa()

// modules
const path = require('path')
const convert = require('koa-convert') // 用于兼容koa1的第三方模块在koa2环境使用
const static = require('koa-static') // 指定静态资源位置
const views = require('koa-views') // 模板引擎
const bodyParser = require('koa-bodyparser') // 方便获取Post的data
const logger = require('koa-logger') // 日志记录
const session = require('koa-session-minimal') // 会话
const mysqlStore = require('koa-mysql-session')  // 用于连接koa跟mysql的会话模块

// database
const config = require('../config')

// routes
const routers = require('./routers/index')

// MySQL存储session相关配置
const sessionMysqlConfig = {
  user: config.database.USERNAME,
  password: config.database.PASSWORD,
  database: config.database.DATABASE,
  host: config.database.HOST
}

// 配置session中间件
app.use(session({
  key: 'USER_SID',
  store: new mysqlStore(sessionMysqlConfig)
}))

// 配置log中间件
app.use(convert(logger()))

// 配置获取Post Data的中间件
app.use(bodyParser())

// 配置静态资源 
app.use(convert(static(
  path.join(__dirname, '../static')
)))

// 配置服务端渲染引擎所使用的模板信息
app.use(views(path.join(__dirname, './views'), {
  extension: 'ejs'
}))

// 配置路由的中间件
app.use(routers.routes()).use(routers.allowedMethods())

app.listen(config.port, () => {
  console.log(`Listening to ${config.port}`)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant