在授权路由中使用express.static中间件 [英] Using express.static middleware in an authorized route
问题描述
我正在使用带有express和passportjs的节点来限制访问位于私人文件夹中的文件。我已经将我的代码缩短到了以下。
公共静态文件夹中的所有东西都很好,但是通过使用staticMiddleware返回404错误来定位私有文件夹的路由。
I'm using node with express and passportjs to restrict access to files located in a private folder. I have reduced my code to the following. Everything in the public static folder works great but route targeting the private folder through the use of the staticMiddleware returns 404 errors.
var express = require('express')
, util = require('util');
var app = express.createServer();
var staticMiddleware = express.static(__dirname + '/private');
app.configure(function() {
app.use(app.router);
app.use(express.logger('dev'));
app.use('/public',express.static(__dirname + '/public'));
});
app.get('/private/:file', function(req, res, next){
console.log('about to send restricted file '+ req.params.file);
staticMiddleware(req, res, next);
});
app.listen(16000);
我正在使用以下引用,似乎适用于别人,所以我一定是缺少一些东西。
对于我在私人区域内的内容只显示404个回复,这不行。
I was using the following references that seems to work for others, so I must be missing something. It won't work for me showing only 404 responses for the content located in the private area.
NodeJS不会提供静态文件,即使使用express.static
我可以宣誓就这样工作,可能是在新版本的东西中被打破了。
I could have sworn I had this working before, maybe it was broken in a new version of something.
- 节点v0.8.1
- npm 1.1.12
- express@2.5.11
- connect@1.9.2
- Node v0.8.1
- npm 1.1.12
- express@2.5.11
- connect@1.9.2
推荐答案
sheesh盯着我整个时间
sheesh staring at me the whole time
app.get('/private/:file', function(req, res, next){
console.log('about to send restricted file '+ req.params.file);
req.url = req.url.replace(/^\/private/, '')
staticMiddleware(req, res, next);
});
编辑11-29-2014
Edit 11-29-2014
所以在有人张贴这个问题后,我回到这个答案,发现即使我提到了护照,我从来没有展示过我如何使用这个功能。
So after someone posted to the question I came back to this answer to find that even though I mention passportjs I never showed how I ended up using this function.
var staticMiddlewarePrivate = express['static'](__dirname + '/private');
app.get('/private/*/:file', auth.ensureAuthenticated, function(req, res, next){
console.log('**** Private ****');
req.url = req.url.replace(/^\/private/, '');
staticMiddlewarePrivate(req, res, next);
});
这篇关于在授权路由中使用express.static中间件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!