0

Using hbs as described in the nestjs docs works fine with the top level app controller.

But for controllers which are located in subfolders the static content are not rendered / found by hbs.

main.ts (as usual):

async function bootstrap() {
  try {
    const app = await NestFactory.create<NestExpressApplication>(AppModule);
    ...
    app.useStaticAssets(join(__dirname, '..', 'public'));
    app.setBaseViewsDir(join(__dirname, '..', 'views/web'));
    app.setViewEngine('hbs');
    ...
  } catch (error) {
    ...
  }
}
bootstrap();

folder structure:

public
  common.css
  work1_logo.png
src
  work1
    work1.controller.ts
    work1.module.ts
    work1.service.ts
  app.controller.ts
  app.module.ts
  app.service.ts
  main.ts
  ...
views
  web
    template1.hbs

In the app.controller.ts, hbs renders the template1.hbs using common.css and work1_logo.png In the work1.controller.ts, hbs renders the template1.hbs WITHOUT common.css and work1_logo.png.

Conclusion:

It seems that

app.setBaseViewsDir(join(__dirname, '..', 'views/web'));

is working well, but not

app.useStaticAssets(join(__dirname, '..', 'public'));

Any ideas what I can do to keep my folder structure?

(Otherwise all controller have to be located on top level. But this cannot be the idea of a modular structure.)

joerg
  • 1

0 Answers0