【问题标题】:Express web server no responseExpress 网络服务器无响应
【发布时间】:2020-07-16 08:15:03
【问题描述】:

我正在尝试通过 Angular Universal 在服务器端渲染中运行我的 Angular 应用程序。

这是Angular Universal生成的JS文件,我只是编辑了路径和端口:

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';

// The Express app is exported so that it can be used by serverless Functions.
export function app() {
  const server = express();
  const distFolder = process.cwd();
  const indexHtml = existsSync(join(distFolder, 'index.original.html')) ? 'index.original.html' : 'index';

  // Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)
  server.engine('html', ngExpressEngine({
    bootstrap: AppServerModule,
  }));

  server.set('view engine', 'html');
  server.set('views', distFolder);

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

  // All regular routes use the Universal engine
  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 || 4001;

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

// Webpack will replace 'require' with '__webpack_require__'
// '__non_webpack_require__' is a proxy to Node 'require'
// The below code is to ensure that the server is run only when not requiring the bundle.
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';

文件夹结构很好,我在服务器上运行:

> node .\server.js
Node Express server listening on http://localhost:4001

当我点击该网站时,它只会显示加载,直到超时。

Express 在控制台上记录:

DEPRECATED: DI is instantiating a token "ngmodule_material_carousel_MatCarouselHammerConfig" that inherits its @Injectable decorator but does not provide one itself.
This will become an error in v10. Please add @Injectable() to the "ngmodule_material_carousel_MatCarouselHammerConfig" class.
(node:6896) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
DEPRECATED: DI is instantiating a token "ngmodule_material_carousel_MatCarouselHammerConfig" that inherits its @Injectable decorator but does not provide one itself.
This will become an error in v10. Please add @Injectable() to the "ngmodule_material_carousel_MatCarouselHammerConfig" class.

编辑:

从项目中删除 @ngmodule/material-carousel library 后,我仍然收到 Buffer 警告,但现在我可以看到该网站。

【问题讨论】:

  • 试试app.get('*', () => {}),而不是app.get(() => {})。即去掉不必要的'*'

标签: node.js angular express server-side-rendering angular-universal


【解决方案1】:

问题出在material-carousel 库上。我必须将必要的文件从project's GitHub 复制到我的项目中。需要以下文件:

carousel-slide.component.html
carousel-slide.component.scss
carousel-slide.component.ts
carousel-slide.ts
carousel.component.html
carousel.component.scss
carousel.component.ts
carousel.ts
_mixins.scss

这些组件需要 Angular 材质,因此如果您没有在项目中使用 Angular 材质而不是 ng add @angular/material 会更改您的项目,请运行以下命令:

npm install @angular/material @angular/cdk @angular/animations

最后将必要的组件添加到您的模块中:

@NgModule({
  declarations: [
    ...
    MatCarouselComponent,
    MatCarouselSlideComponent
  ],
  imports: [
    ...
    MatButtonModule,
    MatIconModule
  ],
  ...
})
export class AppModule { }

【讨论】:

    【解决方案2】:

    有一个open issue on github,看起来这个库不支持角度通用。

    作为一种解决方法,如果您真的想使用该库,如果您是服务器端,则可以避免渲染轮播

    component.html

    <mat-carousel *ngIf="isBrowser" ></mat-carousel>
    

    component.ts

    import { isPlatformBrowser } from '@angular/common';
    import {PLATFORM_ID} from '@angular/core';
    
    public isBrowser: boolean;
    constructor( @Inject(PLATFORM_ID) public platformId)
    {
        this.isBrowser = isPlatformBrowser(this.platformId);
    }      
    

    【讨论】:

      猜你喜欢
      • 2018-06-22
      • 2017-06-24
      • 1970-01-01
      • 2016-11-01
      • 1970-01-01
      • 2018-01-22
      • 2015-07-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多