文件结构
其实文件结构啥的是为了业务逻辑更加清晰
index是主页面,从default继承.
default里面也include了includes文件里的内容,一个是css样式link,一个是js的源文件script
index.pug
extends ./layouts/default
block title
title Koa douban 首页
block content
.container
.row
.col-md-8
h1 hi #{you}
p this is #{me}
.col-md-4
p 测试动态 pug 模板引擎
default.pug
doctype html
html
head
meta(charset="utf-8")
mate(name='viewport',content='width=device-width,initial-scale=1')
block title
include ../includes/style
body
block content
include ../includes/script
style.pug
link(fref="https://cdn.bootcss.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" rel='stylesheet')
//- script(src='https://cdn.bootcss.com/jquery/3.2.0/jquery.min.js')
//- script(src='https://cdn.bootcss.com/bootstrap/4.0.0-beta.2/js/bootstrap.bundle.min.js')
script.pug
link(fref="https://cdn.bootcss.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" rel='stylesheet')
script(src='https://cdn.bootcss.com/jquery/3.2.0/jquery.min.js')
script(src='https://cdn.bootcss.com/bootstrap/4.0.0-beta.2/js/bootstrap.bundle.min.js')
index.js渲染页面
const koa = require("koa")
const app = new koa()
// const {normal} = require("./tpl")
// const ejs = require("ejs")
const views = require("koa-views")
const { resolve } = require("path")
app.use(views(resolve(__dirname,"./views"),{
extension:"pug" //只要是后缀名为pug的就会识别为模板文件
}))
app.use(async (ctx, next) => {
await ctx.render("index",{
you:"zhangyue",
me:"yuezhang"
})
})
app.listen(4455)