【问题标题】:Express serving index.html from Angular html5 mode not working从 Angular html5 模式快速服务 index.html 不起作用
【发布时间】:2017-03-27 00:05:01
【问题描述】:

为了 SEO,我正在尝试在 Angular JS 应用程序中使用 html5 模式。但是,我遇到了 Express 服务 index.html 请求的问题。深层链接无效,无法刷新页面。

App.js

app.use(serveStatic(path.join(__dirname, 'public')));
app.use(serveStatic(path.join(__dirname, 'node_modules')));
app.use(serveStatic(path.join(__dirname, 'app_client')));
app.use('/api', routesApi);

app.all('/*', function(req, res, next) {
res.sendFile('./app_client/index.html', { root: __dirname });
console.log("send index");
});

我已经尝试了许多来源的研究,例如

 app.get('/', function(req, res) {
 res.sendFile('index.html');
 });

 router.get('/', function(req, res) {
 res.sendFile(path.join(__dirname, 'app_client', 'index.html'));
 });

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

似乎没有一个工作。我知道我的代码一定有问题,但我不知道在哪里。

【问题讨论】:

    标签: angularjs html express


    【解决方案1】:

    终于找到答案了

    app.get('/*', function(req, res, next) {
      res.sendFile(__dirname + '/app_client/index.html');
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-15
      • 1970-01-01
      • 1970-01-01
      • 2018-12-16
      • 2011-03-15
      • 2017-07-24
      • 2017-03-04
      相关资源
      最近更新 更多