Express 4:重定向后我无法获取静态文件

Fra*_*o Z 4 node.js express

我在使用 Express 4 和静态文件时遇到问题。成功登录后,我将我的网络应用程序重定向到“/home/userId”,但随后,我在所有静态文件上收到 404 错误。这是我的路由器:

router.get('/home/:userid', function(req, res, next) {
  // TODO check if token is valid
 User.findById(req.params.userid).exec(function(err, find) {
    if (err) return next(err);
    if (!find) {
      return res.json(utils.createErrorJsonResponse(404, "User not found!"));
    }
    return res.render('home', {
      title: 'Organizator',
      user: find
    });
  })
});
Run Code Online (Sandbox Code Playgroud)

我认为向你展示我的jade文件是没有用的,唯一重要的是有很多导入的脚本,但仅举一个例子,这就是我加载css文件的方式:

  link(href='css/style.css', rel='stylesheet')
Run Code Online (Sandbox Code Playgroud)

这就是我设置静态文件的方式

  app.use(express.static(config.root + '/public',{ maxAge: 86400000 }));
Run Code Online (Sandbox Code Playgroud)

其中“config.root”是我的:

path.normalize(__dirname + '/..')
Run Code Online (Sandbox Code Playgroud)

正如我之前所说,如果我连接到基本页面,那么就我而言:

http://localhost:3000
Run Code Online (Sandbox Code Playgroud)

我所有的静态文件都已导入,但是一旦我重定向,我就会收到 404。那么我该如何修复它呢?例如。我有一个名为“style.css”的样式文件。在控制台的基本页面(' http://localhost:3000 )中我可以看到:

Request URL:http://localhost:3000/css/style.css
Request Method:GET
Status Code:200 OK (from cache)
Run Code Online (Sandbox Code Playgroud)

然后从“ http://localhost:3000/home/ ”:

Request URL:http://localhost:3000/home/css/style.css
Request Method:GET
Status Code:404 Not Found
Run Code Online (Sandbox Code Playgroud)

所以问题是“/home”,但是我如何从静态文件请求中“删除”它?谢谢。

Jos*_*igo 5

正如评论中@Molda 所解释的那样,请确保您的链接以 开头/,否则路径将相对于实际路线http://localhost:3000/home