我无法将我的styles.css链接到我的index.ejs



我在将css样式表链接到index.ejs时遇到问题。我的index.ejs代码被分为_header.ejs、_footer.ejs等文件。我的morganhttp请求记录器中也出现了404错误,它显示:GET /css/style.css 404 152 - 0.537 ms不确定它为什么返回404错误。我的server.js代码看起来像这个

const express = require('express');
const dotenv = require('dotenv')
const morgan = require('morgan')
const mongoose = require('mongoose');
const ejs = require("ejs")
const bodyParser = require("body-parser")
const path = require('path')
const app=express();
dotenv.config({path:'config.env'})
const PORT = process.env.PORT || 8080
// logs requests
app.use(morgan('tiny'));
// parse request to body-parser
app.use(bodyParser.urlencoded({extended: true}))
// set view engine
app.set('view engine', 'ejs')
// load assets
app.use('/css', express.static(path.resolve(__dirname, 'assets/css')))
app.use('/img', express.static(path.resolve(__dirname, 'assets/img')))
app.use('/js', express.static(path.resolve(__dirname, 'assets/js')))

app.get("/", function(req, res) {
res.render('index')
})

app.listen(PORT, () => {
console.log(`Server started on http://localhost:${PORT}`);
})

存储在_Header.ejs中的index.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>Crud App</title>
<script src="https://kit.fontawesome.com/0eca62e181.js" crossorigin="anonymous"></script>
<link rel="stylesheet" href="css/style.css"> 
<!-- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> -->
</head>
<body>
<header>
<nav>
<div class="container">
<div class="text-center">
<a href="/" class="nav-brand text-dark">User Management System</a>
</div>
</div>
</nav>
</header>

另外,这是我的index.ejs:

<!-- include header -->
<%- include('include/_header') %>
<!-- /include header -->

<!-- Main Site -->
<main id="site-main">
<div class="container">
<div class="box-nav d-flex-justify-between">
<a href="/add-user" class="border-shadow">
<span class="text-gradient">New User <i class="fa-solid fa-user"></i></span>
</a>
</div>
<form action="/" method="POST">
<table class="table">
<thead class="thead-dark">
<tr>
<th>ID</th>
<th>Name</th>
<th>@Email</th>
<th>Gender</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<%- include('include/_show') %>
</tbody>
</table>
</form>
</div>
</main>
<!-- /Main Site -->
<!-- include footer -->
<%- include('include/_footer') %>
<!-- /include footer -->

我的文件结构如下:文件结构

当我将其链接到我的index.html中时,我的样式就会显示出来,这与我的index.ejs相同,但没有分离到不同的文件中。该路径是CCD_ 2。我所有的html显示也都正常。任何建议都很好。非常感谢。

解决方案

const express = require('express');
const ejs = require("ejs")
const app=express();
const PORT = process.env.PORT || 3000
// set view engine
app.set('view engine', 'ejs')
// load assets
app.use( express.static(__dirname+'/assets/css'))//✅
app.use(  express.static(__dirname+ '/assets/img'))//✅
app.get("/", function(req, res) {
res.render('index')
})
app.listen(PORT, () => {
console.log(`Server started on http://localhost:${PORT}`);
})

和视图文件夹中的index.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">
<link rel="stylesheet" href="./style.css">
<title>Document</title>
</head>
<body>
<div><img src="./01.jpg" alt=""></img></div> 
</body>
</html>

当您使用app.use( express.static(__dirname+'/public)时,表示该地址(public folder(已被识别用于快速

最新更新