FrozenPandaz / ng-universal-demo

171 stars 54 forks source link

Angular Express Engine service error #25

Open daniDevKr opened 7 years ago

daniDevKr commented 7 years ago

I have developed a service for manage the status codes of the server responses reading this page: Angular Express Engine

:


import { Injectable, Inject } from '@angular/core';
import { Request, Response } from 'express';
import { REQUEST, RESPONSE } from '@nguniversal/express-engine/tokens';

@Injectable()
export class ResponseService {
  constructor(@Inject(RESPONSE) private response: Response) {

      this.response.statusCode = 400;
  }

  public err(status: number){
       this.response.statusCode = status;
  }

I have provide this service at Express-engine in the main.server.ts :

. . .

app.engine('html', ngExpressEngine({
  bootstrap: ServerAppModule,
   providers: [
      ResponseService
    ]
}));

. . .

then i have provided the service to the server-app.module :

. . .
  providers: [
    {
      provide: APP_BOOTSTRAP_LISTENER,
      useFactory: onBootstrap,
      multi: true,
      deps: [
        ApplicationRef,
        TransferState
      ]
    },
    ResponseService
  ],
. . .

at the end i have injected the service in the app.component.ts :

import {Component, OnInit, Inject} from '@angular/core'
import {TransferState} from '../modules/transfer-state/transfer-state';
import {ResponseService} from './response.service';
import {Request} from 'express';
@Component({
    selector: 'demo-app',
    template: `
    <h1>Universal Demo</h1>
    <a routerLink="/">Home</a>
    <a routerLink="/lazy">Lazy</a>
    <router-outlet></router-outlet>
  `,
    styles: [
        `h1 {
      color: green;
    }`
    ]
})
export class AppComponent implements OnInit {
    constructor(private cache: TransferState, @Inject(ResponseService) private responseService: ResponseService) {}

    ngOnInit() {
        this.cache.set('cached', true);

        this.responseService.err(404);

    }
}

I start the server and when i navigate the page with my browser (Google chrome), appears this error in the developers console :

view.js:18 Uncaught Error: Cannot find module "fs"
    at webpackMissingModule (view.js:18)
    at Object.<anonymous> (view.js:18)
    at __webpack_require__ (bootstrap 37d5860…:49)
    at Object.<anonymous> (application.js:22)
    at Object.<anonymous> (application.js:644)
    at __webpack_require__ (bootstrap 37d5860…:49)
    at Object.<anonymous> (express.js:17)
    at __webpack_require__ (bootstrap 37d5860…:49)
    at Object.webpackEmptyContext.keys (index.js:11)
    at __webpack_require__ (bootstrap 37d5860…:49)
webpackMissingModule    @   view.js:18
(anonymous) @   view.js:18
__webpack_require__ @   bootstrap 37d5860…:49
(anonymous) @   application.js:22
(anonymous) @   application.js:644
__webpack_require__ @   bootstrap 37d5860…:49
(anonymous) @   express.js:17
__webpack_require__ @   bootstrap 37d5860…:49
webpackEmptyContext.keys    @   index.js:11
__webpack_require__ @   bootstrap 37d5860…:49
Object.defineProperty.value @   home-view.component.ts:9
__webpack_require__ @   bootstrap 37d5860…:49
(anonymous) @   http.es5.js:2183
__webpack_require__ @   bootstrap 37d5860…:49
hasOwn  @   platform-browser-dynamic.es5.js:166
__webpack_require__ @   bootstrap 37d5860…:49
(anonymous) @   response.service.ts:6
__webpack_require__ @   bootstrap 37d5860…:49
(anonymous) @   bootstrap 37d5860…:147
(anonymous) @   bootstrap 37d5860…:147

Someone knows the solution to this problem ?? 👍