Система не может найти указанный файл. (ошибка ОС 2) в Deno - PullRequest
0 голосов
/ 18 июня 2020

Рендеринг HTML, CSS и Stati c Файлы внутри Deno с шаблоном View Engine
MyCode:
Структура папки

-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.e js:

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

общедоступный /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();

при запуске этого кода обозначает запуск --allow- net --allow-read server.ts . http://localhost: 8000 / работает нормально, но в http://localhost: 8000 / test я получаю эту ошибку:

Система не может найти указанный файл. (ошибка ОС 2) в Deno

1 Ответ

0 голосов
/ 18 июня 2020

Все запросы на получение / тестовый путь перехватываются stati c промежуточным программным обеспечением содержимого, переместите эту функцию промежуточного программного обеспечения после к общей настройке промежуточного программного обеспечения

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