简体   繁体   English

使用 express.static 后未加载 EJS 文件

[英]EJS file not loading after using express.static

I have some plain HTML/CSS files in my frontend folder and the ejs files in the views folder and every other ejs file loads perfectly but the index.ejs file I don't know what I am doing wrong here我的frontend文件夹中有一些纯 HTML/CSS 文件,views 文件夹中有 ejs 文件,其他所有 ejs 文件都可以完美加载,但 index.ejs 文件我不知道我在这里做错了什么

This is my server.js: (this is a basic version of my server file)这是我的 server.js:(这是我的服务器文件的基本版本)

const app = express();

app.set('views', __dirname + '/views');
app.set("view engine", "ejs");
app.use(express.static(__dirname + "/frontend"));

app.get("/", (req, res) => {
  res.render("index");
});

//The below file loads perfectly. (requireAuth is a authentication middleware)
app.get("/create", requireAuth, (req, res) => {
  res.render("create");
});

Also both the files index.ejs and create.ejs are very similar文件 index.ejs 和 create.ejs 也非常相似

If anyone requires any extra details then they can comment down.如果有人需要任何额外的细节,那么他们可以发表评论。

You've defined static middleware for / (root path) and the index page is also defined for root path, but because static middleware is defined early, express tries to find and return static file instead of rendering index page.您已经为/ (根路径)定义了 static 中间件,并且还为根路径定义了索引页面,但是由于 static 中间件定义较早,Express 会尝试查找并返回 static 文件,而不是呈现索引页面。

You have two variants here:你在这里有两个变体:

  1. Define static middleware for some path different than root:为不同于 root 的某些路径定义 static 中间件:
    app.use("/public", express.static(__dirname + "/frontend"));
  2. Don't use root path for index page: app.get("/index", (req, res)...不要为索引页面使用根路径: app.get("/index", (req, res)...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM