[英]Linked css file in jade not loading
This is what my directory structure looks like 这是我的目录结构
--votingApp
app.js
node_modules
public
css
mystyle.css
views
test.jade
mixins.jade
I have written some general purpose blocks in mixins.jade. 我已经在mixins.jade中编写了一些通用块。 test.jade is the main file.'biolerplate','nav','nav item' are different blocks in mixins.
test.jade是主文件。'biolerplate','nav','nav item'是mixins中的不同块。 This is what test.jade looks like
这就是test.jade的样子
include mixins
doctype html
html(lang="en")
head
+boilerplate
link(rel="stylesheet",type="text/css",href="../public/css/mystyle.css")
body
+nav("Voting app","navigation_menu")
+nav_item("#","active") Home
+nav_item("#") Signup
+nav_item("#") Login
And this is my app.js file 这是我的app.js文件
var express=require('express');
var path=require('path');
var app=express();
app.use(express.static(path.join(__dirname,'public')));
app.set('views','./views');
app.set('view engine','jade');
app.get('/',function(req,res){
res.render('test.jade');
});
app.listen(8000);
The problem is that the mystyle.css is not being loaded.The network option in the developer console of mozilla is showing that error 404 for the request for mystyle.css(request url- http://localhost:8000/public/css/mystyle.css ) What should I do please help?Thanks for reading. 问题是未加载mystyle.css.mozilla开发人员控制台中的网络选项显示请求mystyle.css的错误404(请求url- http:// localhost:8000 / public / css / mystyle.css )我该怎么办?谢谢您的阅读。
You don't need the relative path when loading your css, that's why you've defined your public directory as a static resource . 加载CSS时不需要相对路径,这就是为什么将公共目录定义为静态资源的原因 。
Update your link line to this: 将您的链接行更新为:
link(rel="stylesheet",type="text/css",href="/public/css/mystyle.css")
只需使用:
link(rel="stylesheet",type="text/css",href="/css/mystyle.css")
Relative path is never work with node.js so make it absolute like 相对路径永远无法与node.js配合使用,因此请使其绝对
link(rel="stylesheet",type="text/css",href="css/mystyle.css")
and you set your directory as public folder using following line 并使用以下行将目录设置为公用文件夹
app.use(express.static(path.join(__dirname,'public')));
so discard writing public css in path. 因此,请不要在路径中写入公共CSS。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.