Skip to content

Commit

Permalink
fix: not found after not router set.
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Feb 13, 2022
1 parent c17f049 commit 27cc7ae
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/web-express/src/framework.ts
Expand Up @@ -96,7 +96,7 @@ export class MidwayExpressFramework extends BaseFramework<
debug('[express]: use 404 not found middleware');
// eslint-disable-next-line
this.app.use(function notFound(req, res, next) {
next(new httpError.NotFoundError());
next(new httpError.NotFoundError(`${req.path} Not Found`));
});

debug('[express]: use global error handler middleware');
Expand Down
4 changes: 2 additions & 2 deletions packages/web-express/test/index.test.ts
Expand Up @@ -201,7 +201,7 @@ describe('/test/feature.test.ts', () => {
.get('/11');
expect(result.status).toEqual(200);
expect(result.body).toEqual({
'message': 'Not Found',
'message': '/11 Not Found',
'status': 404
});

Expand All @@ -221,7 +221,7 @@ describe('/test/feature.test.ts', () => {
.get('/11');
expect(result.status).toEqual(200);
expect(result.body).toEqual({
'message': 'Not Found',
'message': '/11 Not Found',
'status': 404
});

Expand Down
8 changes: 4 additions & 4 deletions packages/web-koa/src/framework.ts
Expand Up @@ -24,7 +24,7 @@ import * as Router from '@koa/router';
import type { DefaultState, Middleware, Next } from 'koa';
import * as koa from 'koa';
import { Server } from 'net';
import { setupOnerror } from './onerror';
import { setupOnError } from './onerror';

const COOKIES = Symbol('context#cookies');

Expand Down Expand Up @@ -83,13 +83,13 @@ export class MidwayKoaFramework extends BaseFramework<
});

const onerrorConfig = this.configService.getConfiguration('onerror');
setupOnerror(this.app, onerrorConfig, this.logger);
setupOnError(this.app, onerrorConfig, this.logger);

// not found middleware
const notFound = async (ctx, next) => {
await next();
if (!ctx._matchedRoute) {
throw new httpError.NotFoundError();
if (!ctx._matchedRoute && ctx.body === undefined) {
throw new httpError.NotFoundError(`${ctx.path} Not Found`);
}
};

Expand Down
2 changes: 1 addition & 1 deletion packages/web-koa/src/onerror.ts
Expand Up @@ -9,7 +9,7 @@ import {
} from './utils';
import { Utils } from '@midwayjs/decorator';

export function setupOnerror(app, config, logger) {
export function setupOnError(app, config, logger) {
const errorOptions = Object.assign(
{
// support customize accepts function
Expand Down
@@ -0,0 +1,3 @@
{
"name": "ali-demo"
}
@@ -0,0 +1,9 @@
'use strict';

export const keys = 'key';

export const hello = {
a: 1,
b: 2,
d: [1, 2, 3],
};
@@ -0,0 +1,24 @@
import { Configuration, App } from '@midwayjs/decorator';
import { join } from 'path';
import { Application } from '../../../../src';

@Configuration({
imports: [
require('../../../../src')
],
importConfigs: [
join(__dirname, './config')
]
})
export class ContainerConfiguration {

@App()
app: Application;

async onReady() {
this.app.useMiddleware(async (ctx, next) => {
ctx.body = 'abc';
return await next();
});
}
}
11 changes: 10 additions & 1 deletion packages/web-koa/test/index.test.ts
Expand Up @@ -288,7 +288,7 @@ describe('/test/feature.test.ts', () => {
.get('/11');
expect(result.status).toEqual(200);
expect(result.body).toEqual({
'message': 'Not Found',
'message': '/11 Not Found',
'status': 404
});

Expand Down Expand Up @@ -333,4 +333,13 @@ describe('/test/feature.test.ts', () => {
expect(result.status).toEqual(200);
await closeApp(app);
});

it('should test just exists middleware router and not throw error', async () => {
const app = await creatApp('base-app-middleware-router');
const result = await createHttpRequest(app)
.get('/');
expect(result.status).toEqual(200);
expect(result.text).toEqual('abc');
await closeApp(app);
});
});
4 changes: 2 additions & 2 deletions packages/web/src/framework/web.ts
Expand Up @@ -94,8 +94,8 @@ export class MidwayWebFramework extends BaseFramework<
// not found middleware
const notFound = async (ctx, next) => {
await next();
if (!ctx._matchedRoute) {
throw new httpError.NotFoundError();
if (!ctx._matchedRoute && ctx.body === undefined) {
throw new httpError.NotFoundError(`${ctx.path} Not Found`);
}
};
// insert error handler
Expand Down

0 comments on commit 27cc7ae

Please sign in to comment.