【发布时间】:2019-09-03 20:34:17
【问题描述】:
我无法在 express.js 中的 res.sendfile 上同时发送 css 和 html 文件。
index.html
<div class="hdngTab">
<h1 style="align-content: center">Automation Utility</h1>
</div>
index.css
.hdngTab{
background-color: rgb(60, 120, 120);
box-sizing: border-box;
border: solid rgb(60, 120, 120);
text-align: center
}
index.js
const express = require("express");
const app = express();
const http = require("http").Server(app).listen(3000);
console.log("Server Started");
app.get("/", function (req, res) {
res.sendFile(__dirname + "/index.html");
}
)
我根据一些在线参考尝试了以下选项,但没有任何帮助:
-
在根目录传递 index.css,因为浏览器应该能够自己加载它。
app.get('/index.css', function(req, res) { res.sendFile(__dirname + "/" + "index.html"); }); 创建新文件夹并将我的静态文件(html 和 css) 移动到该文件夹下,并将 res.sendfile 替换为 app.use(express .static("文件夹名"));
还有其他可能的解决方法吗?
【问题讨论】: