2015-08-15 85 views
0

我使用express + node.js + Jade。我写了一个玉文件,但我不能让背景图像出现。我打得四处体类,它似乎文本颜色的工作,但在玉石中使用背景图像

body { 
    font-family: "Josefin Slab" !important; 
    background-image: url("concursum-bg.jpg"); 
    background-size: cover; 
    color: white; 
    text-align: center; 
    background-repeat: no-repeat; 
    } 

我确信该图像是在同一文件夹,因为这玉文件时,它不使用背景图片。

当我查看网页源代码的HTML是:

body { 
font-family: "Josefin Slab" !important; 
background-image: url("concursum-bg.jpg"); 
background-size: cover; 
color: white; 
text-align: center; 
background-repeat: no-repeat; 
} 

我在本地主机上运行此,如果该事项。

回答

2

必须提供图像文件,以便浏览器可以访问该图像文件。当它位于带有jade文件的文件夹中(我假设这是一个views目录),它不是由Express提供的,因此浏览器无法访问它。为了解决这个问题,使服务器所服务的映像文件:

  • 在主快速应用,使用express.static从目录提供静态文件: app.use(express.static(__dirname + "/public"));
  • 现在,创建目录要服务的文件来自: mkdir public
  • 创建图像目录那里,理智: mkdir public/images
  • 将有文件 mv {views,public/images}/concursum-bg.jpg
  • 更新您的样式表,将url("concursum-bg.jpg");更改为url("/images/concursum-bg.jpg");
  • 重新启动您的快速应用程序。

http://expressjs.com/starter/static-files.html

+0

会在哪里我创建了 “公共” 目录?在视图所在的目录中? – SocketPhys

+0

它的工作原理,但我想知道为什么快递无法在视图目录中提供服务? – SocketPhys

+0

@SocketPhys Express默认情况下不会这样做,而且它确实没有任何理由。 – Aehmlo