Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

【服务端渲染】ejs

前面用koa-views指定html渲染的引擎是ejs,也装上了ejs,下面看如何在路由响应中,返回服务端渲染页面

  • 根目录下

    • 新建client目录,用于存放客户端的模板
      mkdir client
      
      • 里面新建一个hello.html文件
  • hello.html内容

    <script type="text/javascript" src='/static/js/test.js'></script>
    <p><%= title %></p>
    
  • 在响应路由内使用

    router.get('/hello',async (ctx)=>{
        let title = 'hello koa2'
        await ctx.render('client/hello',{
            title
        })  
    })