首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >系统找不到指定的文件。(操作系统错误2)在Deno中

系统找不到指定的文件。(操作系统错误2)在Deno中
EN

Stack Overflow用户
提问于 2020-06-18 06:09:21
回答 1查看 1.2K关注 0票数 1

使用视图引擎模板在Deno中呈现HTML、CSS和静态文件

MyCode:

文件夹结构

代码语言:javascript
复制
-public
  -css
     -main.css
  -js
     -app.js
-views
  -index.ejs
-server.ts

server.ts

代码语言:javascript
复制
/*  Introduction To Rendering HTML,CSS and Static Files inside Deno with View Engine Template */

// Importing Required Files And Packages Here.
import { Application, Router, send } from "https://deno.land/x/oak/mod.ts";
import {
  viewEngine,
  engineFactory,
  adapterFactory,
} from "https://deno.land/x/view_engine/mod.ts";

// Initializing App Here.
const app = new Application();

//  Setting Up Ejs Templating Engine Here.
const ejsEngine = engineFactory.getEjsEngine();
const oakAdapter = adapterFactory.getOakAdapter();

// Serving A Static Folder Here.
app.use(async (ctx,next)=>{
    await send(ctx,ctx.request.url.pathname,{
        root :`${Deno.cwd()}/public`
    })
    next();
})

// Initializing Router Here.
const router = new Router();

// Defining Routes Here.
router.get("/test", (ctx) => {
  ctx.render("views/index.ejs", {
    payload: {
      text: "test",
    },
  });
}).get("/", (ctx) => {
    ctx.render("views/index.ejs", {
      payload: {
        text: "<h1>Introduction To Templating Engines In Deno. </h1>",
      },
    });
  });



// MiddleWares Here.
app.use(viewEngine(oakAdapter, ejsEngine));
app.use(router.routes());
app.use(router.allowedMethods());

console.log("Server Started On Port Number 8000.");
await app.listen({ port: 8000 });

// Run : deno run --allow-net --allow-read server.ts

视图/index.ejs :

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Templating Engines</title>
    <link rel="stylesheet" href="/css/main.css" />
    <script src="/js/app.js" defer></script>
  </head>
  <body>
    <h1>Welcome To Deno World!</h1>
    <div><%- payload.text %></div>
    <p id="content"></p>
  </body>
</html>

public/css/main.css

代码语言:javascript
复制
p{
    color:red;
}

public/js/app.js

代码语言:javascript
复制
const content = document.getElementById("content");

const getDataJsFile = () => {
  content.innerHTML = `
    Java script Dummy Text  ::::::==>
    Lorem ipsum dolor sit amet consectetur adipisicing elit. Nesciunt quaerat
    perferendis consectetur doloribus repellendus ullam corrupti explicabo
    placeat reprehenderit ad.`;
};

getDataJsFile();

当我运行这段代码时,deno运行--允许-net--允许读取server.ts。http://localhost:8000/运行良好,但在http://localhost:8000/test中,我得到了以下错误:

系统找不到指定的文件。Deno

中的操作系统错误2

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-18 09:05:58

所有对/test路由的获取请求都被静态内容中间件截获,然后将中间件功能转移到通用中间件设置中。

代码语言:javascript
复制
app.use(viewEngine(oakAdapter, ejsEngine));
app.use(router.routes());
app.use(router.allowedMethods());

// Serving A Static Folder Here.
app.use(async (ctx,next)=>{
  await send(ctx,ctx.request.url.pathname,{
      root :`${Deno.cwd()}/public`
  })
  next();
})
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62443440

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档