First of all, after creating a views folder where I specified in main.ts, I added my contract.hbs file in it, but I was getting an error that main.hbs cannot be found in layouts. Then I copied contract.ts and pasted it as main.hbs under views/layouts directory. Then I pasted it into dist and the rendering process worked. However, when I was making a few changes, I realized that the changes I made were not applied to the file I was rendering and while trying a few things, I got the error I showed again. I see the problem as the file not being copied to dist, but it could be something else since I don’t fully understand it.
views path: /Users/beratkaraaslan/Documents/pluspayprojects/pluspay-api-panel/views
Error;
Error: Failed to lookup view "contract" in views directory "/Users/beratkaraaslan/Documents/pluspayprojects/pluspay-api-panel/dist/views"
at Function.render (/Users/beratkaraaslan/Documents/pluspayprojects/pluspay-api-panel/node_modules/express/lib/application.js:597:17)
at ServerResponse.render (/Users/beratkaraaslan/Documents/pluspayprojects/pluspay-api-panel/node_modules/express/lib/response.js:1048:7)
at htmlContent (/Users/beratkaraaslan/Documents/pluspayprojects/pluspay-api-panel/app/routes/settings/controller/documents.controller.ts:46:21)
at new Promise (<anonymous>)
at DocumentsController.getContract (/Users/beratkaraaslan/Documents/pluspayprojects/pluspay-api-panel/app/routes/settings/controller/documents.controller.ts:45:39)
at async /Users/beratkaraaslan/Documents/pluspayprojects/pluspay-api-panel/node_modules/@nestjs/core/router/router-execution-context.js:46:28
at async /Users/beratkaraaslan/Documents/pluspayprojects/pluspay-api-panel/node_modules/@nestjs/core/router/router-proxy.js:9:17 {
view: View {
defaultEngine: 'hbs',
ext: '.hbs',
name: 'contract',
root: '/Users/beratkaraaslan/Documents/pluspayprojects/pluspay-api-panel/dist/views',
engine: [Function: bound renderView] AsyncFunction,
path: undefined
}
}
HttpError: Failed to lookup view "contract" in views directory "/Users/beratkaraaslan/Documents/pluspayprojects/pluspay-api-panel/dist/views"
main.ts;
app.useStaticAssets(join(__dirname, '.', 'public'))
app.setBaseViewsDir(join(__dirname, '.', 'views'));
app.setViewEngine('hbs');
app.engine('hbs', hbs({ extname: 'hbs' }));
controller.ts;
async getContract(
@Res() res: Response,
@BossPlusAuthUser() user: JwtUserBossPlus,
@Query() signature?: GetContractQueryDto
) {
try {
const data = await this.service.getContract(user, signature?.signature);
const htmlContent = await new Promise<string>((resolve, reject) => {
res.render('contract', data, (err, html) => {
if (err) {
reject(err);
} else {
resolve(html);
}
});
});
const base64Content = Buffer.from(htmlContent).toString('base64');
res.json({ base64: base64Content });
} catch (error) {
console.error(error);
throw new HttpError(error.message, error.status);
}
}
nest-cli.json;
{
"collection": "@nestjs/schematics",
"sourceRoot": "app",
"compilerOptions": {
"assets": [
{
"include": "views/**/*",
"outDir": "dist/views"
}
]
}
}
package.json;
"scripts": {
"postbuild": "cp -R views dist/views",
"start:dev": "TZ=Europe/Istanbul nest start --watch",
...
...
...
}