
Template engines are the backbone of web development, providing a powerful and flexible way to create dynamic web pages. NestJS makes it easy to integrate your favorite template engine, allowing you to focus on building your application's logic instead of worrying about the nitty-gritty details of rendering HTML. With NestJS and your preferred template engine, you can create beautiful and functional web pages that delight your users and keep them coming back for more - ChatGPT
$ npm i -g @nestjs/cli
$ nest new nestJsTemplate
$ cd nestJsTemplate
$ nest generate module reports
$ nest generate controller reports
$ nest generate service reports
import { Controller, Get, Param, Res } from '@nestjs/common';
import { ReportsService } from './reports.service';
import * as empData from 'src/data/sample.json';//static json file
import { Response } from 'express';
@Controller('reports')
export class ReportsController {
constructor(private reportsService: ReportsService) { }
@Get()
getEmployeeReports(): any {
// return [{id : 0}];
return this.reportsService.findAll();
}
@Get(':id')
getEmployeeReportsById(@Param('id') id: string): any {
return this.reportsService.findById(id);
}
@Get('emp/:id')
async getEmpReports(@Param('id') empId: string): Promise<Object> {
var report = empData;
return report;
}
@Get('emps/:id') // method to process the html template content
async getEmpReportsAsTemplate(@Param('id') empId: string, @Res() res: Response) {
var report = empData;
return res.render(
"report.hbs", { emp: empData.employeeDtls, dept: empData.departments },
);
}
}
Listing 1.1: Code snippet for the controller class
<body>
<h1>Employees - Reporting Info</h1>
<table id="reports">
<tr>
<th>Employee Id </th>
<th>First Name </th>
<th>Last Name </th>
<th>email </th>
<th>Hired Date</th>
</tr>
{{#each emp}}
<tr>
<td>{{employeeId}}</td>
<td>{{firstName}}</td>
<td>{{lastName}}</td>
<td>{{email}}</td>
<td>{{hireDate}}</td>
</tr>
{{/each}}
</table>
<h2>Department Details</h2>
<table id="reports">
<tr>
<th>Department Id </th>
<th>Department Name </th>
<th>Email </th>
<th>Department Manager </th>
</tr>
{{#each dept}}
<tr>
<td>{{id}}</td>
<td>{{deptName}}</td>
<td>{{email}}</td>
<td>{{departmenttManager}}</td>
</tr>
{{/each}}
</table>
</body>
Listing 1.2: Code snippet of report.hbs file
import { NestFactory } from '@nestjs/core';
import { ValidationPipe } from '@nestjs/common';
import { NestExpressApplication } from '@nestjs/platform-express';
import { resolve } from 'path';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(
AppModule);
app.useStaticAssets(resolve('./src/public'));
app.setBaseViewsDir(resolve('./src/views'));
app.setViewEngine('hbs');
app.useGlobalPipes(new ValidationPipe({ whitelist: true, transform: true }));
app.setGlobalPrefix('api/v1');
await app.listen(process.env.PORT || 3000);
}
bootstrap();
Listing 1.3: Code snippet of the main module file
{
"employeeDtls": [
{
"employeeId": 100,
"firstName": "John",
"lastName": "Chen",
"email": "john.chen@###.com",
"hireDate": "2008-10-16"
},
{
"employeeId": 101,
"firstName": "Ameya",
"lastName": "Job",
"email": "ameya.job@$###.com",
"hireDate": "2013-03-06"
},
{
"employeeId": 102,
"firstName": "Pat",
"lastName": "Fay",
"email": "pat.fey@###.com",
"hireDate": "2001-03-06"
}
],
"departments": [
{
"id": "1",
"deptName": "Payroll",
"email": "payroll@###.com",
"departmenttManager":"Adam Liser"
},
{
"id": "2",
"deptName": "Admininstrator",
"email": "admin@###.com",
"departmenttManager":"Riner Saw"
}
]
}
Listing 1.4: JSON sample data
_schema-version: 3.3.0
ID: my-sample-nestjs
version: 0.0.1
modules:
- name: my-sample-nestjs
type: nodejs
path: .
build-parameters:
builder: custom
commands:
- npm ci
- npm run build
- npm prune --include=dev
parameters:
buildpack: nodejs_buildpack
command: node dist/main
memory: 128M
routes:
- route: my-sample-nestjs-${space}.${default-domain}
- route: my-sample-nestjs-${space}.cert.${default-domain}
Listing 1.5: Code snippet of mta.yml file
$ cf login
$ mbt build -t ./
$ cf deploy my-sample-nestjs_0.0.1.mtar
Listing 1.6: Command to build and deploy the app in SAP BTP
Fig-1: API response screenshots
Few of my other blogs:-
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
7 | |
3 | |
3 | |
2 | |
2 | |
1 | |
1 | |
1 | |
1 | |
1 |