-
Notifications
You must be signed in to change notification settings - Fork 402
Closed
Labels
status: needs triageIssues which needs to be reproduced to be verified report.Issues which needs to be reproduced to be verified report.
Description
When use @Render
in Koa, it will break all api request, all API request will return a html template.
How to produce
- Create a html render controller
// html/default.ts
@Controller()
export default class DefaultController {
@Get('/homepage')
@Render('home/index')
async index() {
return {
title: config.app.name,
};
}
}
- Create a api json controller
// api/default.ts
@JsonController()
export default class DefaultController {
@Get("/api/users")
getAll() {
return {
code: 0,
data: [{ name: 'Jim Green', id: 1 }]
};
}
- Test
curl http://localhost/api/users // show json as expected
curl http://localhost/homepage // show HTML page as expected
curl http://localhost/api/users // show HTML page (!!NOT EXPECTED!!)
Reason code
routing-controllers/src/driver/koa/KoaDriver.ts
Lines 245 to 251 in 2455688
} else if (action.renderedTemplate) { // if template is set then render it // TODO: not working in koa | |
const renderOptions = result && result instanceof Object ? result : {}; | |
this.koa.use(async function (ctx: any, next: any) { | |
await ctx.render(action.renderedTemplate, renderOptions); | |
}); | |
} |
in KoaDriver.ts, handleSuccess will register a render middleware. so that every request will goes to the middleware, even a JSON api.
GodEngine
Metadata
Metadata
Assignees
Labels
status: needs triageIssues which needs to be reproduced to be verified report.Issues which needs to be reproduced to be verified report.