NodeJS,使用friendly时找不到css,js url
NodeJS, can't find css, js when using friendly url
这是我关于 Whosebug 的第一个问题,我的英语很糟糕,对此感到抱歉。
在 nodejs 应用程序中,当我使用 url 如“/post_detail?id=123”时
app.get('/post_detail', function(req, res) {
res.render("post_detail", {id: req.param("id")})
});
没关系,我会在 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 - -
但是,当我使用“/post_detai/123”和路由器时:
app.get('/post_detail/:id', function(req, res) {
res.render("post_detail", {id: req.param("id")})
});
在控制台中出现以下错误:
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
那么,如何解决这个问题呢?请帮我。 :(
感谢您的帮助。
P/S:当我在控制台中使用 url“/post_detail/123”时,是否有任何区别:
Url "/post_detail?id=123"
GET /css/themes.css 304 7.313 ms - -
Url "/post_detail/123"
GET /post_detail/css/themes.css 404 38.535 ms - 965
看起来您在包含 js
和 css
文件时最有可能使用相对路径而不是绝对路径。
在您的视图文件中(也许 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>
当您在 /post_detail?id=123
时,相对路径可以正常工作,因为它们是相对于 /
但当您访问 /post_detail/123
时,它们现在是相对于 /post_detail/
并且将请求前缀为 /post_detail/
.
的资源
所以尝试在您的视图中使用绝对路径,我希望它有效。
这是我关于 Whosebug 的第一个问题,我的英语很糟糕,对此感到抱歉。
在 nodejs 应用程序中,当我使用 url 如“/post_detail?id=123”时
app.get('/post_detail', function(req, res) {
res.render("post_detail", {id: req.param("id")})
});
没关系,我会在 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 - -
但是,当我使用“/post_detai/123”和路由器时:
app.get('/post_detail/:id', function(req, res) {
res.render("post_detail", {id: req.param("id")})
});
在控制台中出现以下错误:
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
那么,如何解决这个问题呢?请帮我。 :(
感谢您的帮助。
P/S:当我在控制台中使用 url“/post_detail/123”时,是否有任何区别: Url "/post_detail?id=123"
GET /css/themes.css 304 7.313 ms - -
Url "/post_detail/123"
GET /post_detail/css/themes.css 404 38.535 ms - 965
看起来您在包含 js
和 css
文件时最有可能使用相对路径而不是绝对路径。
在您的视图文件中(也许 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>
当您在 /post_detail?id=123
时,相对路径可以正常工作,因为它们是相对于 /
但当您访问 /post_detail/123
时,它们现在是相对于 /post_detail/
并且将请求前缀为 /post_detail/
.
所以尝试在您的视图中使用绝对路径,我希望它有效。