configuration快递为每个url发送index.html除了那些以.css和.js结尾的文件

我是Express的新手,我试图build立一个SPA,每个URL都由index.html(Backbone)处理。

我希望每个URL都发送到index.html,除了/bundle.js和/style.css–或者更好的是,任何URL可以指示一个文件(以.xyz结尾)

我试过了:

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

但是,发送的index.php的内容bundle.js。 我该怎么做呢?

我相信可能有两种方法可以解决这个问题,首先可能是可取的。 如果您可以移动bundle.jsstyle.css ,请将它们和其他任何静态文件放置在public目录中,并使用以下方法静态提供public所有文件:

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

这种方法是可取的,因为当你将新的静态文件放在public目录中时它会“正常工作”。 然后,您应该能够访问这些静态文件在http:// server:port / bundle.js (或根据您select的层次结构适当的子文件夹)

或者,您可以保持文件结构不变,并使用路由的定义顺序来完成类似的行为,尽pipe它不太灵活,并且基本上是静态定义的:

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