其他分享
首页 > 其他分享> > Deno 页面渲染

Deno 页面渲染

作者:互联网

#Deno 页面渲染#

视频讲解 https://www.bilibili.com/video/BV1BT4y1E7Nh/?p=2

我们一起来完成以下步骤:

#controllers/controller.ts

const { cwd } = Deno;

class Controller {
    static async getData(ctx: any){
        //cwd获取当前工程目录 
        //注意 ' !== `
        ctx.render(`${cwd()}/views/index.ejs`,{
            title:"Testing",
            data:{name:"deepincoding.com"}
        }); 
    }
}

export default Controller;

#routers/index.ts

import { Router } from "https://deno.land/x/oak/mod.ts";
import Controller from "../controllers/Controller.ts";

const router = new Router();

router.get("/",Controller.getData);

export default 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">
  <title><%= title %></title>
</head>
<body>
  <h2>Hello <%= data.name %></h2>
</body>
</html>

#main.ts

import { Application } from "https://deno.land/x/oak/mod.ts"
import {viewEngine,engineFactory,adapterFactory} from "https://deno.land/x/view_engine/mod.ts";
import router from "./routers/index.ts";

const ejsEngine = engineFactory.getEjsEngine();
const oakAdapter = adapterFactory.getOakAdapter();

const app = new Application();
app.use(viewEngine(oakAdapter,ejsEngine));
app.use(router.routes());
app.use(router.allowedMethods());

await app.listen({port: 8000 })

 

标签:index,const,渲染,ts,Controller,Deno,import,router,页面
来源: https://www.cnblogs.com/JavaWeiBianCheng/p/13070608.html