nestjs / serve-static

Serve static websites (SPA's) using Nest framework (node.js) 🥦
https://nestjs.com/
MIT License
462 stars 57 forks source link

When build with webpack's 'production' mode - httpAdapterHost is not initialized. #110

Closed Serg-Mois closed 4 years ago

Serg-Mois commented 4 years ago

Hello, I've taken your example https://github.com/nestjs/nest/tree/master/sample/24-serve-static, and added webpack builder in order to bundle everything in one file.

When build with 'production' mode set in webpack - there is an exception in runtime:

24-serve-static>npm run start:prod

> nest-typescript-starter@1.0.0 start:prod \24-serve-static
> node dist/main

[Nest] 1736   - 02/20/2020, 2:20:43 PM   [NestFactory] Starting Nest application...
(node:1736) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
[Nest] 1736   - 02/20/2020, 2:20:43 PM   [l] l dependencies initialized +14ms
(node:1736) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'getInstance' of undefined

but, if mode in webpack is commented - everything works fine.

In node_modules\@nestjs\serve-static\dist\serve-static.module.js

let ServeStaticModule = ServeStaticModule_1 = class ServeStaticModule {
    constructor(ngOptions, loader, httpAdapterHost) {
        this.ngOptions = ngOptions;
        this.loader = loader;
        this.httpAdapterHost = httpAdapterHost;

httpAdapterHost in runtime is


{
  httpAdapterHost: {
    ioAdapter: null,
    globalPrefix: '',
    globalPipes: [],
    globalFilters: [],
    globalInterceptors: [],
    globalGuards: [],
    globalRequestPipes: [],
    globalRequestFilters: [],
    globalRequestInterceptors: [],
    globalRequestGuards: []
  }

see code here. https://github.com/Serg-Mois/serve_static_webpack_bug

also, when build with webpack - you need explicitly add "fastify-static" into dependencies.

Serg-Mois commented 4 years ago

Is there any other way to send index.html if no Controllers were found? I was trying to make:

@Controller('')
export class ClientController {

  @Get('*')
  public angularApp(@Res() res: Response): any {
    res.sendFile(path.join(__dirname, './client/index.html'));
  }
}

and put this Controller as the last one in controllers array of Module , but as described here https://github.com/nestjs/nest/issues/1667 - the order doesn't matter.

kamilmysliwiec commented 4 years ago

Don't use the production mode in webpack to bundle server-side applications. This may cause issues with numerous libraries, including typeorm, sequelize-typescript or NestJS.