我有一个安静的快递服务+骨干客户端的目录结构,像这样启用了pushState(客户端代码在public/
)
app.js
lib/routes.js
-- public/
-- index.html
我设置/public
为一个静态目录app.configure
:
app.use(express.static(__dirname + '/public'));
如果首先访问索引,这已经很有效了。
然后,当我直接访问主页以外的路线时,我确保重定向到index.html
. 这在 中工作得很好app.js
,但是如果我尝试将其放入lib/routes.js
我会得到一个禁止错误:
从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'
总结一下如何在不出现 Forbidden 错误的情况下使用sendFile
和传入public/index.html
from ?lib/routes.js