Skip to content

【BUG】koa render will break all API request #610

@Gcaufy

Description

@Gcaufy

When use @Render in Koa, it will break all api request, all API request will return a html template.

How to produce

  1. 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,
    };
  }
}
  1. 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 }]
    };
  }
  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

} 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: needs triageIssues which needs to be reproduced to be verified report.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions