我正在尝试使用node、express和ejs模板制作一个简单的服务器。我已经让服务器指向页面,加载它,甚至能够使用include语句生成其他代码片段。然而,由于某种原因,样式表无法加载。
app.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,包括相对于我的localhost指向的相对路径于app.js、board.ejs甚至只是style.css,但似乎都不起作用。非常感谢任何建议。
'/public'
?设置app.use
会如何改变样式表的评估方式?我还尝试了将其设置为href='/styles/style.css'
,但无论如何都没有成功。 - Loourrlink
标签),根目录是你在 app.use 中定义的public
文件夹,而不是整个应用程序的根目录。在这种情况下,链接标签中 href 中的 "/" 引用的是public
目录,而不是你的应用程序的根目录。 - mumush