在 Node.js 中运行时,CSS 样式不会出现在我的 HTML 中。
目录:
Login_Page_V2.html
登录页面_V1.js
公开
-CSS
-style.css
我的服务器代码:
var express = require('express');
var session = require('express-session');
var bodyParser = require('body-parser');
var path = require('path');
var app = express();
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function(request, response) {
response.sendFile(path.join(__dirname + '/Login_Page_V2.html'));
});
HTML代码:
link rel="stylesheet" type="text/css" href="./public/css/styleProject.css"/
最佳答案
您需要在 /public
提供您的静态中间件。
app.use('/public', express.static(path.join(__dirname, 'public')));
在不添加中间件路径的情况下,express 正在寻找 public
目录中 /public/css/styleProject.css
的 uri(本质上,试图引用该文件在 ${__dirname}/public/public/css/styleProject.css
)。通过指定静态中间件的路径,您可以避免这种重复并提高性能,因为只有以 /public/
为前缀的 uris 才会在文件系统上查找。
关于javascript - CSS 不会出现在 HTML 和 Node 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58468299/