Deno 静态文件CSS或图片

Deno 静态页面或图片

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

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

  1. 沿用之前的工程代码
  2. 新增Login页面和响应的controller
  3. 添加CSS资源文件,然后预览页面

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"}
        }); 
    }

    //登录页面
    static async login(ctx: any){
        ctx.render(`${cwd()}/views/login.ejs`);
    }
}

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);

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

export default router;

views/login.ejs





    Deep In Coding
    
    
    
    
    
    
    
    
    
   




User Icon
Warning!请确认邮箱或密码

main.ts




import { Application,send} 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());

app.use(async ctx =>{
    await send(ctx,ctx.request.url.pathname,{
        root:`${Deno.cwd()}/static`
    });
});

await app.listen({port: 8000 })

你可能感兴趣的:(deno)