首页 > 解决方案 > 当我渲染除主页之外的不同路线时,html 没有加载源,express-handlebars-Node.js

问题描述

在此处输入图像描述我在我的 Node.js 项目中使用 express-handlebars。我正确设置了 express-handlebars。主页加载成功,没有错误但是当我渲染其他路由时 html 没有加载 css 和 js:

http://localhost:4000/user/signup(我只有普通格式)

 http://localhost:4000/user/stylesheets/style.css net::ERR_ABORTED 404 (Not Found)
    GET http://localhost:4000/user/images/logo.png 404 (Not Found)
    GET http://localhost:4000/user/javascripts/jquery-3.3.1.min.js net::ERR_ABORTED 404 (Not Found)
    GET http://localhost:4000/user/javascripts/bootstrap.bundle.min.js net::ERR_ABORTED 404 (Not Found)
    GET http://localhost:4000/user/javascripts/all.js net::ERR_ABORTED 404 (Not Found)

快速车把设置

//layout.hbs:
<head>
  <title>{{title}}</title>
  <link rel='stylesheet' href='stylesheets/style.css' type="text/css" />
</head>
<body>
  {{> navbar}} //partial
  {{{body}}}
  <script src="javascripts/jquery-3.3.1.min.js"> </script>
  <script src="javascripts/bootstrap.bundle.min.js"> </script>
  <script src="javascripts/all.js"> </script>
</body>
</html>
//user.js
router.get("/user/signup", (req, res) => {
  res.render("user/signup", { csrfToken: req.csrfToken() });
});

谢谢您的帮助!

标签: javascriptcssnode.js

解决方案


您好,您能否分享您的目录,例如哪个文件夹包含哪个文件。我认为问题出在这一行。如果您的 style.css 在任何文件夹中。您应该这样提及

例如,如果我的 style.css 文件在 public/css/style.css 我会这样写

<link rel='stylesheet' href='/css/style.css' type="text/css" />

不像这样

<link rel='stylesheet' href='css/style.css' type="text/css" />

我希望它会工作。谢谢


推荐阅读