#unable to find views and public directory :(

8 messages · Page 1 of 1 (latest)

keen stone
#

so my project directory is as follows:
project-name/
├── src/
| └── ...
├── public/
| ├── images/
| | └── ...
| ├── scripts/
| | └── ...
| └── styles/
| └── style.css
└── views/
└── index.hbs

main.ts:

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { NestExpressApplication } from '@nestjs/platform-express';
import { join } from 'path';

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule);

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

  app.setViewEngine('hbs');
  app.setBaseViewsDir(join(__dirname, '..', 'views'));

  await app.listen(3000);
}

bootstrap();

app.controller.ts:

import { Controller, Get, Render } from '@nestjs/common';

@Controller()
export class AppController {
  @Get()
  @Render('index') 
  getHello() {
    const data = {
      title: 'My HTML and CSS Application',
      message: 'Welcome to my application!',
    };
    return data;
  }
}

I keep getting the error 'cannot find views directory inside dist'. The views is not inside dist, I know it's supposed to compile upon running 'npm run start' but it does not transfer. Here is the full traceback:

[Nest] 8324  - 03/08/2023, 16:07:25   ERROR [ExceptionsHandler] Failed to lookup view "index" in views directory "C:\Users\Aleksa Hadzic\OneDrive\Documents\importante\jadore\dist\views"
glad frigate
#

Looks like things get compiled to dist/src/stuff instead of dist/stuff. To make this more consistent you can use path.join(process.cwd(), 'public') (and the same for views) instead of using __dirname

keen stone
#

jadore is the project name

#

I am not sure how to execute path.join

#

it can't find 'path'

#

sorry new to ts and nest

glad frigate
#

import * as path from 'path'

keen stone
#

That worked! Thank you very much!