使用 EJS 时样式 sheet 没有链接

Style sheet is not getting linked while using EJS

我正在学习使用ejs、express、node js。我正在将我的风格 sheet 改成我的 header 这是我的代码,这是 . I am using header and footer as include. Here is my

我的app.js-

const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const app = express();
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({
    extended: true
}));
app.use(express.static("public"));
app.get("/", function(req, res) {
    res.render("home");
});
app.listen(3000, function() {
    console.log("Server started on port 3000");
});

这里有2个注意事项

style.css 是外部 css 文件。所以你不需要那个文件里面的样式标签。它应该只包含 css.

在您的 Express 应用程序中,您必须提及 public 提供静态文件的目录。喜欢css/js/image

可以通过

完成

app.use(express.static(__dirname + '/public'));

假设您将 css 文件放入应用程序根目录中的 public 文件夹中。现在您必须在模板文件中引用 css 文件,例如

<link href="/css/style.css" rel="stylesheet" type="text/css"> 在这里,我假设您已将 css 文件放入 public 文件夹内的 css 文件夹中。

因此文件夹结构将是

.
./app.js
./public
    /css
        /style.css

适当关闭head标签