express.js handlebars,获取静态文件目录根据url保持变化
express.js handlebars, get static files directory keep changes according to url
我是新手。基本上我的问题很简单。
我想从一个 public 目录提供像 /css javascript 这样的文件..
layout.hbs
<html>
<head>
<link href="css/style.css" rel="stylesheet" />
<script src="js/app.js"></script>
</head>
<body>
{{{body}}}
</body>
</html>
router.js --> 我有三个路由指向一个 index.hbs
router.get("/", function(req,res){
res.render("index.hbs");
})
router.get("/articles", function(req,res){
res.render("index.hbs");
})
router.get("/articles/show/:id", function(req,res){
res.render("index.hbs");
})
现在的问题是当我 运行 这个地址时:
curl "http://localhost:3000/"
http://localhost:3000/js/app
http://localhost:3000/css/style.css
----------------------------------------------
curl "http://localhost:3000/articles"
http://localhost:3000/articles/js/app
http://localhost:3000/articles/css/style.css
----------------------------------------------
curl "http://localhost:3000/show/1"
http://localhost:3000/show/1/js/app
http://localhost:3000/show/1/css/style.css
注意 /css 和 /js 路径根据 UrlRequest 不断变化。如何防止这种情况发生?
我正在使用 express 和 handlebars,并且已经设置了我的静态文件
app.use(express.static(path.join(__dirname, 'public');
您应该在模板中将 JS 和 CSS 的网址指定为绝对网址:使用 /css/style.css
.
而不是 css/style.css
第一个意思是"use this path relative to this page to browsers (and curl), the second "使用此路径相对于主机 - 这就是你想要的。
我是新手。基本上我的问题很简单。 我想从一个 public 目录提供像 /css javascript 这样的文件..
layout.hbs
<html>
<head>
<link href="css/style.css" rel="stylesheet" />
<script src="js/app.js"></script>
</head>
<body>
{{{body}}}
</body>
</html>
router.js --> 我有三个路由指向一个 index.hbs
router.get("/", function(req,res){
res.render("index.hbs");
})
router.get("/articles", function(req,res){
res.render("index.hbs");
})
router.get("/articles/show/:id", function(req,res){
res.render("index.hbs");
})
现在的问题是当我 运行 这个地址时:
curl "http://localhost:3000/"
http://localhost:3000/js/app
http://localhost:3000/css/style.css
----------------------------------------------
curl "http://localhost:3000/articles"
http://localhost:3000/articles/js/app
http://localhost:3000/articles/css/style.css
----------------------------------------------
curl "http://localhost:3000/show/1"
http://localhost:3000/show/1/js/app
http://localhost:3000/show/1/css/style.css
注意 /css 和 /js 路径根据 UrlRequest 不断变化。如何防止这种情况发生?
我正在使用 express 和 handlebars,并且已经设置了我的静态文件
app.use(express.static(path.join(__dirname, 'public');
您应该在模板中将 JS 和 CSS 的网址指定为绝对网址:使用 /css/style.css
.
css/style.css
第一个意思是"use this path relative to this page to browsers (and curl), the second "使用此路径相对于主机 - 这就是你想要的。