UNPKG

879 BPlain TextView Raw
1import { Request as ERequest, Response as EResponse } from "express";
2import Response from "./response";
3
4/**
5 * Generate an HTML response using the template engine.
6 */
7export default class RenderResponse extends Response {
8 private view: string;
9 private context: any;
10
11 /**
12 * The constructor
13 * @param view the path to the view
14 * @param context the context necessary to generate the template
15 */
16 constructor(view: string, context: any) {
17 super();
18 this.view = view;
19 this.context = context;
20 }
21
22 performResponse(req: ERequest, res: EResponse) {
23 if (!this.context) {
24 this.context = {};
25 }
26 if ((req as any).lang) {
27 this.context.lang = (req as any).lang;
28 } else {
29 this.context.lang = "it";
30 }
31 res.render(this.view, this.context);
32 }
33}