我不知道這是否有幫助,但如果你想要的是有選擇地記錄或拒絕靜態文件的下載,你可以這樣做:
至上,保證路由前執行靜態中間件:
app.configure(function(){
...
app.use(app.router); // this one goes first
app.use(express.static(__dirname + '/public'));
...
其次,註冊捕獲所有請求並只是有條件地響應的路由。下面的示例檢測並記錄在文件a.txt中(該文件系統路徑是/public/file-A.txt)將下載的消息,任何其他文件的要求將不會中斷下載:
app.get('/*', function(req, res, next){
if(req.params[0] === 'file-A.txt') { // you can also use req.uri === '/file-A.txt'
// Yay this is the File A...
console.warn("The static file A has been requested")
// but we still let it download
next()
} else {
// we don't care about any other file, let it download too
next()
}
});
這它,我希望這有助於。
爲什麼不在'static'後添加'*'404? – Raynos