I'm just starting with nest.js having some previous experience with node and express.
I'm trying to set up a default layout for my partials and response renders. Here's my main.ts
:
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import * as hbs from 'hbs';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.setBaseViewsDir(__dirname + '/views');
hbs.registerPartials(__dirname + '/views/partials');
app.setViewEngine('hbs');
await app.listen(3000);
}
bootstrap();
I have a feeling that there should be a default layout defined somewhere in that code but I have no idea how to proceed. My Google-fu has failed me, so can anyone give me a hand with this?
HandleBars can be used to render web pages to the client side from data on the server-side. To use handlebars in express, we need to store HTML code into a . hbs extension in the 'views' folder in the source directory as hbs looks for the pages in the views folder. Now, we need to change the default view engine.
NestJS Application Using CQRS Design Pattern.
Nest. JS helps build lightweight, well-structured and amazing microservices and helps evolve the technology stack. The microservice architecture enables the rapid, frequent and reliable delivery of large, complex applications. Out-of-the-box tools and features make development, extension, and maintenance efficient.
You can use
app.set('view options', { layout: 'index' });
You still need to use @Render('valid-layout')
on all controllers and the provided layout name must be valid. It will just be overwritten by your default layout.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With