[英]NodeJS, can't find css, js when using friendly url
this is my first question on stackoverflow, and my english so bad, sorry about that. 这是我关于stackoverflow的第一个问题,我的英语不好,对此感到抱歉。
In nodejs application, when i using url like "/post_detail?id=123" 在nodejs应用程序中,当我使用类似“ / post_detail?id = 123”的网址时
app.get('/post_detail', function(req, res) {
res.render("post_detail", {id: req.param("id")})
});
It's ok, i will load all js and css in post_detail page: 没关系,我将在post_detail页面中加载所有js和CSS:
GET /css/main.css 304 7.194 ms - -
GET /css/themes.css 304 7.313 ms - -
GET /js/vendor/bootstrap.min.js 304 8.139 ms - -
GET /js/plugins.js 304 8.810 ms - -
GET /js/app.js 304 16.648 ms - -
But, when i using "/post_detai/123" and router: 但是,当我使用“ / post_detai / 123”和路由器时:
app.get('/post_detail/:id', function(req, res) {
res.render("post_detail", {id: req.param("id")})
});
In console gives the below errors: 在控制台中给出以下错误:
GET /post_detail/css/themes.css 404 38.535 ms - 965
GET /post_detail/css/main.css 404 38.817 ms - 965
GET /post_detail/js/vendor/bootstrap.min.js 404 28.060 ms - 965
GET /post_detail/js/plugins.js 404 19.449 ms - 965
GET /post_detail/js/app.js 404 16.454 ms - 965
So, how to solve this problem? 那么,如何解决这个问题呢? Please help me. 请帮我。 :( :(
Thanks for your help. 谢谢你的帮助。
P/S: when i'm using url "/post_detail/123", in console is there any difference: Url "/post_detail?id=123" P / S:当我使用网址“ / post_detail / 123”时,在控制台中有什么不同:网址“ / post_detail?id = 123”
GET /css/themes.css 304 7.313 ms - -
Url "/post_detail/123" 网址“ / post_detail / 123”
GET /post_detail/css/themes.css 404 38.535 ms - 965
It looks like you are most likely using relative paths rather than absolute paths when including your js
and css
files. 包括js
和css
文件时,您似乎最有可能使用相对路径而不是绝对路径。
In your view file (perhaps layout.html
, I'm only guessing at the structure of your project) try using urls prefixed with a forward slash: 在您的视图文件(也许是layout.html
,我只是在猜测项目的结构)中,请尝试使用以正斜杠为前缀的网址:
<!-- This -->
<link href="/css/main.css">
<script src="/js/app.js"></script>
<!-- Rather than this -->
<link href="css/main.css">
<script src="js/app.js"></script>
When you are on /post_detail?id=123
the relative paths work properly, since they are relative to /
but when you visit /post_detail/123
they are now relative to /post_detail/
and will request the resources with a prefixed /post_detail/
. 当您在/post_detail?id=123
,相对路径可以正常工作,因为它们是相对于/
但是当您访问/post_detail/123
它们现在相对于/post_detail/
,并将请求带有前缀/post_detail/
的资源。
So try and use absolute paths in your view and I hope it works. 因此,请尝试在您的视图中使用绝对路径,希望它能起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.