我正在尝试为模板制作一个带有 node、express 和 ejs 的简单服务器。我已经让服务器指向页面,加载它,甚至可以使用 include 语句生成其他代码。但是由于某种原因,样式表不会加载。
应用程序.js
var express = require('express'),
app = express(),
http = require('http'),
server = http.createServer(app),
fs = require('fs');
var PORT = 8080;
app.set('view engine', 'ejs');
app.get('/', function(req, res){
res.render('board.ejs', {
title: "anything I want",
taco: "hello world",
something: "foo bar",
layout: false
});
});
app.listen(PORT);
console.log("Server working");
ejs 文件位于目录views/board.ejs 中
<html>
<head>
<title><%= title %></title>
<link rel='stylesheet' href='../styles/style.css' />
</head>
<body >
<h1> <%= taco %> </h1>
<p> <%= something %> </p>
</body>
</html>
并且 style.css 位于相对于 app.js 的 styles/style.css 目录中
p {
color:red;
}
我已经尝试了我可以为链接的 href 设想的每条路径,包括相对于我的本地主机相对于 app.js 相对于 board.ejs 指向的位置,甚至只是 style.css,但似乎没有一个有效。非常感谢任何建议。