加载单独的CSS样式表(heroku nodejs)

我无法为本地和在Heroku服务器上的heroku web应用程序加载单独的css样式表。 我最近从谷歌应用程序引擎迁移过来,我加载样式表的方式工作得很好。 我在这里做错了什么? 任何帮助将不胜感激!! 这是我的代码:

/app/server.js

var express = require("express"); var app = express(); app.use(express.logger()); var fs = require("fs"); var buf = fs.readFileSync("html/index.html"); var index = buf.toString(); app.get('/', function(request, response) { response.send(index); }); var port = process.env.PORT || 5000; app.listen(port, function() { console.log("Listening on " + port); }); 

/app/html/index.html

 <!DOCTYPE html> <html> <head> <title>Starters Singapore | Social Group Dating</title> <link rel="stylesheet" type="text/css" href="../css/index.css"> </head> <body> <div id="testdiv">This is a test</div> </body> </html> 

/app/css/index.css

 body{ margin:0; text-align: left; background-color: #666666; } #testdiv{ width: 50%; height: 50%; background-color: #00a1ec; } 

您使用的是相对path的CSS文件。 这个path是相对于服务器的根,而不是HTML文件。 我没有Heroku NodeJS的经验,但是我认为path应该是

/css/index.css

我也在Heroku上部署应用程序。 我会build议使用ejs或玉来呈现你的html。 我更喜欢ejs。 既然你使用Express,我也会使用这个参考来创build一个“public”目录。 在这里输入图像描述

然后在你的layout.ejs (对于使用ejs-locals npm的Express 2.xx或Express 3.xx)

 <!DOCTYPE html> <html> <head> <title>The Title</title> <% stylesheet('/css/index.css') %> <%- stylesheets%> </head> <body> </body> </html> 

你也可以在index.ejs中调用这个layout.ejs

 <%- layout('layout') %> 

我也build议在使用带有ejs的布局时引用这个 。 学习ejs或者玉石起初可能稍微有些烦人,但是一旦你理解得更好,它就会很好用。 希望这不是太多的负载的答案。