错误错误:无效的服务器端渲染

时间:2020-02-20 16:30:16

标签: node.js angular server-side-rendering angular9

我正在使用此命令以SSR模式构建和运行我的项目。

npm run build:ssr && npm run serve:ssr

构建成功完成,服务器正确启动,但是在对http://localhost:4000/的第一个请求中,我收到此错误:

ERROR Error: invalid
at ɵɵinvalidFactoryDep (/Users/david/Documents/tracegaming/web/dist/web/server/main.js:1:4792305)
at NodeInjectorFactory.ToastyComponent_Factory [as factory] (ng:///ToastyComponent/ɵfac.js:3:43)
at getNodeInjectable (/Users/david/Documents/tracegaming/web/dist/web/server/main.js:1:4819897)
at instantiateAllDirectives (/Users/david/Documents/tracegaming/web/dist/web/server/main.js:1:4857508)
at createDirectivesInstances (/Users/david/Documents/tracegaming/web/dist/web/server/main.js:1:4857745)
at ɵɵelementStart (/Users/david/Documents/tracegaming/web/dist/web/server/main.js:1:4942423)
at Module.ɵɵelement (/Users/david/Documents/tracegaming/web/dist/web/server/main.js:1:4943502)
at AppComponent_Template (/Users/david/Documents/tracegaming/web/dist/web/server/main.js:1:3105548)
at executeTemplate (/Users/david/Documents/tracegaming/web/dist/web/server/main.js:1:4856642)
at renderView (/Users/david/Documents/tracegaming/web/dist/web/server/main.js:1:4852371)

这将返回内部服务器错误。

这是我的 server.ts 文件

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

import { ngExpressEngine } from '@nguniversal/express-engine';
import * as express from 'express';
import { join } from 'path';

import { AppServerModule } from './src/main.server';
import { APP_BASE_HREF } from '@angular/common';
import { existsSync } from 'fs';
import 'localstorage-polyfill'

global['localStorage'] = localStorage;
export function app() {
  const server = express();
  const distFolder = join(process.cwd(), 'dist/web/browser');
  const indexHtml = existsSync(join(distFolder, 'index.original.html')) ? 'index.original.html' : 'index';
  server.engine('html', ngExpressEngine({
    bootstrap: AppServerModule,
  }));

  server.set('view engine', 'html');
  server.set('views', distFolder);
  server.get('*.*', express.static(distFolder, {
    maxAge: '1y'
  }));
  server.get('*', (req, res) => {
    res.render(indexHtml, { req, providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }] });
  });

  return server;
}

function run() {
  const port = process.env.PORT || 4000;
  const server = app();
  server.listen(port, () => {
    console.log(`Node Express server listening on http://localhost:${port}`);
  });
}
declare const __non_webpack_require__: NodeRequire;
const mainModule = __non_webpack_require__.main;
const moduleFilename = mainModule && mainModule.filename || '';
if (moduleFilename === __filename || moduleFilename.includes('iisnode')) {
  run();
}

export * from './src/main.server';

0 个答案:

没有答案