koa-mysql(三)

使用模板

目录结构

koa-mysql(三)

修改index.js

const koa = require('koa');
const router = require('koa-router')();
//引进中间件
const path = require('path'); //路径
const views = require('koa-views'); //视图
const ejs = require('ejs'); //模板
const app = new koa();

app.use(views(path.join(__dirname,'./views'),{
    extension: 'ejs'
}))

router.get('/',async(ctx,next) => {
    await ctx.render('index');
})

app.use(router.routes());
app.listen(3000);
console.log(`app start at localhost:3000`);

koa-mysql(三)

结构优化

index.js

const koa = require('koa');
const router = require('koa-router')();
const path = require('path');
const views = require('koa-views');
const ejs = require('ejs');
const app = new koa();

app.use(views(path.join(__dirname,'./views'),{
    extension: 'ejs'
}))


app.use(require('./routers/index.js').routes());
app.use(require('./routers/detail.js').routes());
app.listen(3000);
console.log(`app start at localhost:3000`);

koa-mysql(三)

routers/index.js

const router = require('koa-router')();
router.get('/',async(ctx, next) => {
    await ctx.render('index')
})

module.exports = router

koa-mysql(三)
views/index.ejs

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <div>index</div>
</body>
</html>

koa-mysql(三)

相关推荐