javascript - CSS 不会出现在 HTML 和 Node 中

标签 javascript html css node.js

在 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/

相关文章:

javascript - 使用 Stapes.js 进行继承

HTML.Checkboxfor 到切换按钮 HTML?

Javascript 向下滚动 vh

css - ADF Skin.css - 自定义问题

html - 如何将 HTML5 缓存与图像站点一起使用?

html - CSS 星级评定系统

javascript - AngularJS 路由不渲染模板

javascript - jquery - toFixed 但不是 toFixed

javascript - 覆盖javascript中的全局函数

html - 固定元素 CSS 在 Chrome 和 Firefox 中的不同结果