我正在尝试为模板制作一个包含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,我尝试了所有我能想到的路径,包括相对于我的localhost指向的地方、相对于app.js的地方、相对于board.ejs的地方,甚至只是style.css,但似乎都不起作用。任何建议都非常感谢。
声明静态目录:
app.use(express.static(__dirname + '/public'));
<link rel='stylesheet' href='/style.css' />