当我渲染除主页以外的其他路由时,html不会加载源,express-handlebars-Node.js

时间:2019-06-13 02:32:47

标签: javascript css node.js

enter image description here我在Node.js项目中使用了Express-Handlebars。 我正确设置了express-handlebars.Main页面成功加载,但是当我渲染其他路由时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() });
});

谢谢您的协助!

1 个答案:

答案 0 :(得分:1)

您好,您可以在其中共享目录,例如哪个文件夹包含哪个文件。我认为问题是这行。如果您的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" />

我希望它能起作用。谢谢