Skip to content

Commit fc23ca4

Browse files
fix(ExpressDriver): use response.render to render templates
1 parent d3005c0 commit fc23ca4

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

src/driver/express/ExpressDriver.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ export class ExpressDriver extends BaseDriver implements Driver {
235235
// set http status code
236236
if (result === undefined && action.undefinedResultCode && action.undefinedResultCode instanceof Function) {
237237
throw new (action.undefinedResultCode as any)(options);
238-
}
238+
}
239239
else if (result === null) {
240240
if (action.nullResultCode) {
241241
if (action.nullResultCode instanceof Function) {
@@ -269,7 +269,7 @@ export class ExpressDriver extends BaseDriver implements Driver {
269269
else if (action.renderedTemplate) { // if template is set then render it
270270
const renderOptions = result && result instanceof Object ? result : {};
271271

272-
this.express.render(action.renderedTemplate, renderOptions, (err: any, html: string) => {
272+
options.response.render(action.renderedTemplate, renderOptions, (err: any, html: string) => {
273273
if (err && action.isJsonTyped) {
274274
return options.next(err);
275275

@@ -281,7 +281,7 @@ export class ExpressDriver extends BaseDriver implements Driver {
281281
}
282282
options.next();
283283
});
284-
}
284+
}
285285
else if (result === undefined) { // throw NotFoundError on undefined response
286286
const notFoundError = new NotFoundError();
287287
if (action.undefinedResultCode) {

test/functional/render-decorator.spec.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import "reflect-metadata";
22
import {Controller} from "../../src/decorator/Controller";
33
import {Get} from "../../src/decorator/Get";
4+
import {Res} from "../../src/decorator/Res";
45
import {createExpressServer, createKoaServer, getMetadataArgsStorage} from "../../src/index";
56
import {assertRequest} from "./test-utils";
67
import {Render} from "../../src/decorator/Render";
@@ -24,7 +25,17 @@ describe("template rendering", () => {
2425
name: "Routing-controllers"
2526
};
2627
}
27-
28+
29+
@Get("/locals")
30+
@Render("render-test-locals-spec.html")
31+
locals(@Res() res: any) {
32+
res.locals.myVariable = "my-variable";
33+
34+
return {
35+
name: "Routing-controllers"
36+
};
37+
}
38+
2839
}
2940
});
3041

@@ -62,4 +73,16 @@ describe("template rendering", () => {
6273
});
6374
});
6475

76+
describe("Express should render a template with given variables and locals variables", () => {
77+
assertRequest([3001], "get", "locals", response => {
78+
expect(response).to.have.status(200);
79+
expect(response.body).to.contain("<html>");
80+
expect(response.body).to.contain("<body>");
81+
expect(response.body).to.contain("Routing-controllers");
82+
expect(response.body).to.contain("my-variable");
83+
expect(response.body).to.contain("</body>");
84+
expect(response.body).to.contain("</html>");
85+
});
86+
});
87+
6588
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<html>
2+
3+
<body>
4+
{{ name }}
5+
{{ myVariable }}
6+
</body>
7+
8+
</html>

0 commit comments

Comments
 (0)