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`);
结构优化
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`);
routers/index.js
const router = require('koa-router')(); router.get('/',async(ctx, next) => { await ctx.render('index') }) module.exports = router
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>
相关推荐
webgm 2020-08-16
yezitoo 2020-06-06
始作俑者 2020-01-19
lei0 2020-11-02
igogo00 2020-07-04
iconhot 2020-06-26
88481456 2020-06-18
疯狂紫萧 2020-06-16
SelinaChan 2020-05-15
luckymaoyy 2020-04-25
80437700 2020-04-17
austindev 2020-04-07
我有一只小松鼠 2020-02-29
webfullStack 2020-02-15
binglingnew 2012-10-03
jinxiutong 2020-01-04
Dawnworld 2019-12-23
yezitoo 2019-12-05