NodeJS ejs不加载css文件

7gcisfzg  于 2023-11-17  发布在  Node.js
关注(0)|答案(1)|浏览(127)

正如标题所说,我的layout.ejs文件没有加载style.css文件。我已经doomscrolled现在几个小时没有找到这个问题的答案。我尝试了这么多的东西,并检查了所有的拼写100次。当检查网络选项卡的路径似乎是正确的css文件,但它给了一个404错误。文件在root/public/style/style. css和root/views/layout.ejs
layout.ejs

<link rel="stylesheet" href="/style/style.css">

字符串
main.js

"use strict";

const express = require("express"),
    app = express(),
    router = express.Router(),
    mongoose = require("mongoose"),
    layouts = require("express-ejs-layouts"),
    errorController = require("./controllers/errorController"),
    homeController = require("./controllers/homeController"),
    path = require('path');   

//setting the port and the view engine as .ejs files
app.set("port", process.env.PORT || 3001);
app.set("view engine", "ejs"); 

router.use(express.static(path.join(__dirname, "public")));
router.use(layouts);

router.use(
    express.urlencoded({
        extended: false
        })
    );
router.use(express.json());
  
// using the homecontroller
router.use(homeController.logRequestPaths);
app.get("/", homeController.index);

// Using the error controller
app.use(errorController.ResourceNotFound404);
app.use(errorController.internalError500);

app.use("/", router);

app.listen(app.get("port"), () => {
    console.log(`Server running at http://localhost:${app.get("port")}`)
});


快来帮忙!
已尝试layout.ejs文件中路径的不同版本和express中需要静态文件的不同代码段。

ao218c7q

ao218c7q1#

你能改变:

router.use(express.static(path.join(__dirname, "public")));

字符串

app.use(express.static(path.join(__dirname, "/public")));

相关问题