Skip to content

Commit

Permalink
fix: return ctx.body and set header after send (#738)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 authored Nov 29, 2020
1 parent c8f51f6 commit 4c8e740
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 1 deletion.
6 changes: 6 additions & 0 deletions packages/web-express/src/framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ export class MidwayExpressFramework extends BaseFramework<
const controller = await req.requestContext.getAsync(controllerId);
// eslint-disable-next-line prefer-spread
const result = await controller[methodName].apply(controller, args);

if (res.headersSent) {
// return when response send
return;
}

if (res.statusCode === 200 && (result === null || result === undefined)) {
res.status(204);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,9 @@ export class APIController {
async redirect() {
}

@Get('/ctx-body')
async getCtxBody() {
this.ctx.res.send('ctx-body');
}

}
5 changes: 5 additions & 0 deletions packages/web-express/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ describe('/test/feature.test.ts', () => {
console.log('result.status', result.status);
expect(result.status).toBe(204);
});

it('test get data with ctx.body', async () => {
const result = await createHttpRequest(app).get('/ctx-body');
expect(result.text).toEqual('ctx-body');
});
});

});
9 changes: 8 additions & 1 deletion packages/web-koa/src/framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,14 @@ export abstract class MidwayKoaBaseFramework<
}
const controller = await ctx.requestContext.getAsync(controllerId);
// eslint-disable-next-line prefer-spread
ctx.body = await controller[methodName].apply(controller, args);
const result = await controller[methodName].apply(controller, args);
if (result) {
ctx.body = result;
}

if (!ctx.body) {
ctx.body = undefined;
}

// implement response decorator
if (Array.isArray(routerResponseData) && routerResponseData.length) {
Expand Down
5 changes: 5 additions & 0 deletions packages/web-koa/test/fixtures/base-app/src/controller/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@ export class APIController {
@Get('/login')
@Redirect('/')
async redirect() {}

@Get('/ctx-body')
async getCtxBody() {
this.ctx.body = 'ctx-body';
}
}
5 changes: 5 additions & 0 deletions packages/web-koa/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ describe('/test/feature.test.ts', () => {
const result = await createHttpRequest(app).get('/204');
expect(result.status).toBe(204);
});

it('test get data with ctx.body', async () => {
const result = await createHttpRequest(app).get('/ctx-body');
expect(result.text).toEqual('ctx-body');
});
});

});
5 changes: 5 additions & 0 deletions packages/web/test/feature.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ describe('/test/feature.test.ts', () => {
const result = await createHttpRequest(app).get('/login');
expect(result.status).toEqual(302);
});

it('test get data with ctx.body', async () => {
const result = await createHttpRequest(app).get('/ctx-body');
expect(result.text).toEqual('ctx-body');
});
});

it('should test global use midway middleware id in egg', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,8 @@ export class APIController {
//
}

@Get('/ctx-body')
async getCtxBody() {
this.ctx.body = 'ctx-body';
}
}

0 comments on commit 4c8e740

Please sign in to comment.