The system cannot find the file specified. (os error 2) in Deno

 ̄綄美尐妖づ 提交于 2021-02-11 15:40:26

问题


Rendering HTML,CSS and Static Files inside Deno with View Engine Template
MyCode:
Folder Structure

-public
  -css
     -main.css
  -js
     -app.js
-views
  -index.ejs
-server.ts


server.ts

/*  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

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

p{
    color:red;
}

public/js/app.js

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

when i run this code deno run --allow-net --allow-read server.ts . http://localhost:8000/ is working fine but in http://localhost:8000/test , i am getting this error :

The system cannot find the file specified. (os error 2) in Deno


回答1:


All GET request to /test route is intercepted by static content middleware, move that middleware function after to common middleware setup

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


来源:https://stackoverflow.com/questions/62443440/the-system-cannot-find-the-file-specified-os-error-2-in-deno

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!