Angular 通用只显示第一页的页面源

Angular Universal only showing page source for first page

我在共享网络服务器上有一个 Angular 通用网络应用程序 运行。我 运行 使用我的 DirectAdmin 中名为 NodeJS Selector 的工具。有了这个,我可以 npm install 和 server:ssr server.js 文件。它正在运行,我可以浏览到我的所有页面并看到所有内容。

问题 然而是在页面源代码中。当我转到基础 url (www.example.com/) 并右键单击 => 'view page source' 时,我可以看到主页的内容,就像 Universal 应该做的那样。但是当我转到任何其他页面时,我看不到页面源中的内容,只有 app-root 标记。

我觉得这与路由器模块有关,在我的 server.ts 中,甚至可能与服务器 运行 上的 Apache 中的配置有关,但我不能'不懂是什么。

我也尝试过 运行 我的应用程序与 pm2,但根本没有加载通用。使用 pm2 我只在页面源代码中看到了 app-root 标签。我的 pm2 实例 运行 每天都消失了,即使在 运行 实例上执行 'pm2 save' 也是如此。当我第二天进入 SSH 并执行 'pm2 list' 时,那里什么也没有...所以这就是为什么我切换到 NodeJS 选择器工具 的原因,它现在已经中途工作了。

这是我的应用程序-routing.module.ts(为简洁起见,留下了一些路径和导入):

const routes: Routes = [
  { path: 'generators', component: GeneratorComponent },
  { path: 'about', component: AboutComponent},
  { path: 'disclaimer', component: DisclaimerComponent},
  { path: 'privacy-policy', component: PrivacyPolicyComponent},
  { path: '', component: HomeComponent },

  // otherwise redirect to home
  { path: '**', redirectTo: '' }
];

@NgModule({
  imports: [RouterModule.forRoot(routes, { initialNavigation: 'enabled', scrollPositionRestoration: 'enabled'})],
  exports: [RouterModule]
})
export class AppRoutingModule { }

server.ts:

import 'zone.js/dist/zone-node';

import * as express from 'express';
import {join} from 'path';

// Express server
const app = express();

const PORT = process.env.PORT || 4000;
const DIST_FOLDER = join(process.cwd(), 'dist/browser');

// * NOTE :: leave this as require() since this file is built Dynamically from webpack
const {AppServerModuleNgFactory, LAZY_MODULE_MAP, ngExpressEngine, provideModuleMap} = require('./dist/server/main');

// Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)
app.engine('html', ngExpressEngine({
  bootstrap: AppServerModuleNgFactory,
  providers: [
    provideModuleMap(LAZY_MODULE_MAP)
  ]
}));

app.set('view engine', 'html');
app.set('views', DIST_FOLDER);

// Example Express Rest API endpoints
// app.get('/api/**', (req, res) => { });
// Serve static files from /browser
app.get('*.*', express.static(DIST_FOLDER, {
  maxAge: '1y'
}));

// All regular routes use the Universal engine
app.get('*', (req, res) => {
  res.render('index', { req });
});

// Start up the Node server
app.listen(PORT, () => {
  console.log(`Node Express server listening on http://localhost:${PORT}`);
});

有谁知道为什么只有首页在浏览器的页面源中显示内容,而所有其他页面只显示 app-root 标签?

更新

这是我的 .htaccess 文件,如果有帮助的话:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.html$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.html [L]
</IfModule>

我让我的虚拟主机编辑 Apache 的 httpd.conf 文件,将代理设置为端口 4000,并将文档根目录设置为我的索引文件所在的位置:

 DocumentRoot "/domains/appname.com/public_html/browser"

 <Proxy *>
  Order allow,deny
  Allow from all
 </Proxy>
 ProxyPreserveHost On
 ProxyRequests Off
 ProxyPass / https://localhost:4000/
 ProxyPassReverse / https://localhost:4000/

我是否应该向 Apache 添加更多配置以使其他页面在服务器端呈现?

如果你使用Angular Universal 在服务器端渲染,你需要移除.htaccess 重写规则到index.html。节点服务器现在将处理所有传入路由的请求,因此不应再进行重写。