Express 静态服务 index.html 怎么拒绝呢?

O-m*_*kar 3 node.js express angular

当我从 node express 提供我的 angular 5 应用程序时,当我访问domain.com时,我遇到了这个问题,因为 express static 服务 index.html但我在访问domain.com/something 时它可以正常工作可以帮助我如何解决这个问题

app.use(express.static(path.join(__dirname, 'dist'))); //This line serves index.html how to deny it

app.use("/api", routes);

app.get('*', function(req, res) { //The reason it needs to be * so that it works with angular routes or it wont work

//This is never called when i visit domain.com. but it does when i visit domain.com/somthing

   console.log("i was serverd"); 
   res.sendFile(path.join(__dirname, 'dist/index.html'));
});
Run Code Online (Sandbox Code Playgroud)

提前致谢 :)

O-m*_*kar 8

找到了解决办法

var options = {
  index: false
}

app.use(express.static(path.join(__dirname, 'dist'), options));
Run Code Online (Sandbox Code Playgroud)

index : 发送指定的目录索引文件。设置为 false 以禁用目录索引。

参考:http : //expressjs.com/en/api.html