[英]How to load an external CSS file into the HTML with Express
我正在尝试加载一个包含样式的 html 文件。 我可以加载包含样式的 html 文件吗?
更新:样式通过外部 CSS 文件加载
const express = require('express');
const fs = require('fs');
const app = express();
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
app.listen(3000)
是的,你可以加载它。 包含或链接到 CSS 的HTML 仍然是有效的 HTML。
要加载外部 CSS 文件,您可以使用 express express.static
将它们作为静态文件提供,然后将其引用到 HTML 文件中。
例如:假设这个目录结构,服务public
文件夹
|---node_modules
|---index.html
|---package.json
|---package-lock.json
|---server.js
|---public
|---images
|---js
|---css
|--style.css
服务器.js
app.use(express.static("public"));
app.get("/", function (req, res) {
res.sendFile(path.join(__dirname, "/index.html"));
});
索引.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<link rel="stylesheet" href="css/style.css" />
<title>Document</title>
</head>
<body>
<h1>This is index.html</h1>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.