我遇到了Express 4和静态文件的问题。在成功登录后,我将我的Web应用程序重定向到“/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
});
})
});
我认为展示我的jade文件并没有什么用,唯一重要的是有很多导入的脚本,但是举个例子,这是我如何加载CSS文件:
link(href='css/style.css', rel='stylesheet')
这是我设置静态文件的方法。
app.use(express.static(config.root + '/public',{ maxAge: 86400000 }));
其中'config.root'是我的根目录:
path.normalize(__dirname + '/..')
如我之前所说,如果我连接到基本页面,那么在我的情况下:
http://localhost:3000
所有我的静态文件都已经被导入,但当我重定向后就会出现404错误。那么我该怎么解决呢?例如,我有一个名为 'style.css' 的样式文件。在基本页面('http://localhost:3000)中,从控制台可以看到:
Request URL:http://localhost:3000/css/style.css
Request Method:GET
Status Code:200 OK (from cache)
从 'http://localhost:3000/home/' 开始:
Request URL:http://localhost:3000/home/css/style.css
Request Method:GET
Status Code:404 Not Found
所以问题是'/home', 但是我如何从静态文件请求中“移除”它呢?谢谢。