简体   繁体   English

试图显示 ejs 文件,但它显示的是 index.html

[英]Trying to display ejs file but it displays the index.html instead

I'm using EJS for the first and I'm a bit confused.我第一次使用 EJS,我有点困惑。 What's happening is that I have a list.ejs file created inside my views file.发生的事情是我在我的视图文件中创建了一个 list.ejs 文件。 And a index.html outside I'm using a else if statement to do the logical thinking to the render my EJS file answer but when I call the res.render if I have the index.html, it displays it instead of my list.EJS, there are no errors or anything和 index.html 外面我正在使用 else if 语句进行逻辑思考以呈现我的 EJS 文件答案但是当我调用 res.render 时如果我有 index.html,它会显示它而不是我的列表。 EJS,没有错误或任何东西

app.js应用程序.js

const express = require('express');
const app = express();
const port = 3000;

app.set('view engine', 'ejs');

app.use(express.urlencoded({ extended: true }));
app.use(express.static(__dirname));

app.get('/', (req, res) => {
    let today = new Date();
    let currentDay = today.getDay();
    let day = ""

    if (currentDay === 6 | currentDay === 0) {
        day = "Weekend"
    } else {
        day = "Weekday!"
    }
    res.render('list', {dayOfWeek: day})

})


app.listen(port, () => {
    console.log('Listening on port ' + port);
});

list.ejs列表.ejs

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>To Do List</title>
</head>

<body>
    <h1>To Do List</h1>
    <h2>It's a <%=dayOfWeek%>
    </h2>
</body>

</html>

The index.html is just a h1 Send Help.. index.html 只是一个 h1 发送帮助..

What am I doing wrong?我究竟做错了什么? I removed the index.html file and it works but it shouldn't be like that... Right?我删除了 index.html 文件,它可以工作,但它不应该是那样的……对吗?

Add this code:添加此代码:

 const path=require("path"); app.set('views', path.join(__dirname));

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

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