簡體   English   中英

試圖顯示 ejs 文件,但它顯示的是 index.html

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

我第一次使用 EJS,我有點困惑。 發生的事情是我在我的視圖文件中創建了一個 list.ejs 文件。 和 index.html 外面我正在使用 else if 語句進行邏輯思考以呈現我的 EJS 文件答案但是當我調用 res.render 時如果我有 index.html,它會顯示它而不是我的列表。 EJS,沒有錯誤或任何東西

應用程序.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);
});

列表.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>

index.html 只是一個 h1 發送幫助..

我究竟做錯了什么? 我刪除了 index.html 文件,它可以工作,但它不應該是那樣的……對嗎?

添加此代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM