无法使样式表与Node.js的ejs一起使用 [英] Can't get stylesheet to work with ejs for node.js

查看:86
本文介绍了无法使样式表与Node.js的ejs一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个带有节点,express和ejs模板的简单服务器。我已经让服务器指向该页面,将其加载,甚至能够使用include语句生成其他代码。但是由于某种原因,样式表将无法加载。

I'm trying to make a simple server with node, express and ejs for the template. I've gotten the server to point to the page, load it, and am even able to generate other bits of code with the include statement. However for some reason the style sheet will not load.

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

The ejs file is in a directory 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目录中

and style.css is in a styles/style.css directory relative to app.js

p {
  color:red;
}

我已经尝试过可以想到的每条路径,链接包括相对于我的本地主机相对于app.js相对于board.ejs的相对位置,甚至只是style.css的相对位置,但似乎没有任何作用。任何建议,不胜感激。

I've tried every path that I can conceive of for the href of the link including relative to where my localhost points relative to app.js relative to board.ejs and even just style.css but none seem to work. Any suggestions are greatly appreciated.

推荐答案

声明静态目录:

app.use(express.static(__dirname + '/public'));

<link rel='stylesheet' href='/style.css' />

这篇关于无法使样式表与Node.js的ejs一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆