2014-04-06 9 views
3

我有一個寧靜的快遞服務+骨幹客戶端pushState的像這樣(在public/客戶端代碼)啓用的目錄結構節點快遞路由的sendfile失敗取決於文件的位置

app.js 
lib/routes.js 
-- public/ 
    -- index.html 

我成立了/public到是靜態目錄app.configure

app.use(express.static(__dirname + '/public')); 

如果該索引是第一次訪問這個已經工作了蝙蝠。

然後,當我直接訪問主頁以外的路線時,我確保重定向到index.html。這工作完全正常的app.js但如果我試圖把這個lib/routes.js我得到Forbidden錯誤

app.js正常工作:

app.get('*', function(req, res) { 
    res.sendfile(__dirname + '/public/index.html'); 
}); 

lib/routes.js

res.sendfile(__dirname + '../public/index.html'); 

給我:

Error: Forbidden 
     at SendStream.error (/Users/*****/Sites/myproject/node_modules/express/node_modules/send/lib/send.js:145:16) 
     at SendStream.pipe (/Users/*****/Sites/myproject/node_modules/express/node_modules/send/lib/send.js:307:39) 
     at ServerResponse.res.sendfile (/Users/*****/Sites/myproject/node_modules/express/lib/response.js:345:8) 
     at /Users/*****/Sites/myproject/lib/routes.js:8:7 
     at callbacks (/Users/*****/Sites/myproject/node_modules/express/lib/router/index.js:164:37) 
     at param (/Users/*****/Sites/myproject/node_modules/express/lib/router/index.js:138:11) 
     at pass (/Users/*****/Sites/myproject/node_modules/express/lib/router/index.js:145:5) 
     at Router._dispatch (/Users/*****/Sites/myproject/node_modules/express/lib/router/index.js:173:5) 
     at Object.router (/Users/*****/Sites/myproject/node_modules/express/lib/router/index.js:33:10) 
     at next (/Users/*****/Sites/myproject/node_modules/express/node_modules/connect/lib/proto.js:193:15) 

如果我只是嘗試:

res.sendfile('/public/index.html'); 

它無法找到該文件,我得到:

Error: ENOENT, stat '/public/index.html' 

總之我如何使用sendFilelib/routes.jspublic/index.html傳遞沒有得到一個禁錯誤?

+0

[Express res.sendfile throwing forbidden error]可能重複(http://stackoverflow.com/questions/14594121/express-res-sendfile-throwing-forbidden-error) – Joe

+0

這是因爲'中的相對路徑。 。/公/ index.html'。看到這個笨蛋。 – Joe

回答

9

謝謝@Joe。爲了完整,因爲重複數據刪除技術還不是很清楚,並嘗試不同的相對路徑功能,包括試圖{root: 'somepath'}爲第PARAM沒有工作,這是我做過什麼:

var path = require('path'); 
... 
app.get('*', function(req, res) { 
    res.sendfile(path.resolve('public/index.html')); 
}); 

即使是在lib/目錄它使用解析基地目錄,重新重申相對解決沒有爲我工作,但可能有某種方式做到這一點。

+0

仍然不工作@多米尼克托比亞斯 –