Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.3.8版本使用@middleware不生效 #127

Closed
whit15 opened this issue Sep 29, 2021 · 4 comments
Closed

2.3.8版本使用@middleware不生效 #127

whit15 opened this issue Sep 29, 2021 · 4 comments
Labels
invalid This doesn't seem right

Comments

@whit15
Copy link

whit15 commented Sep 29, 2021

大佬好,我这边准备加一个中间件来处理跨域问题,发现中间件只有在app中使用middlewareOrder: ['cors']全局加载才会生效,而在Controller里使用@Middleware('cors')局部加载不生效,辛苦大佬核实处理一下。

中间件定义

import {
    Daruk,
    DarukContext,
    defineMiddleware,
    injectable,
    MiddlewareClass,
    Next
} from 'daruk';

@defineMiddleware('cors')
class Cors implements MiddlewareClass {
    public initMiddleware(daruk: Daruk) {
        return async (ctx: DarukContext, next: Next) => {
            ctx.set('Access-Control-Allow-Origin', '*');
            ctx.set('Access-Control-Allow-Methods', 'GET,POST,OPTIONS,PUT,PATCH,DELETE');
            ctx.set('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization, x-access-token');

            await next();
        };
    }
}

Controller

import { controller, get, DarukContext, middleware, validate, inject, post, request, summary, responses, Next } from "daruk";
import JWT from "../../utils/JWT";
import Tools from '../../utils/Tools';

@controller()
class AuthController {
    @inject("UserService") private userService;
    @inject("UserRoleService") private userRoleService;
    @inject("TeamService") private teamService;
    @inject("TeamAdminService") private teamAdminService;

    @middleware('cors')
    @post("/admin/login")
    @validate({
        user_id: {
            type: 'string',
            required: true
        },
    })
    public async login(ctx: DarukContext, next: Next) {
        ...
    }
}
@xiaojue
Copy link
Contributor

xiaojue commented Oct 27, 2021

收到

@xiaojue
Copy link
Contributor

xiaojue commented Oct 27, 2021

import {
  Daruk,
  DarukContext,
  defineMiddleware,
  MiddlewareClass,
  DarukServer,
  controller,
  middleware,
  post,
  validate,
  Next,
} from "daruk";

(async () => {
  const myapp = DarukServer();

  @defineMiddleware("cors")
  class Cors implements MiddlewareClass {
    public initMiddleware(daruk: Daruk) {
      return async (ctx: DarukContext, next: Next) => {
        ctx.set("Access-Control-Allow-Origin", "*");
        ctx.set(
          "Access-Control-Allow-Methods",
          "GET,POST,OPTIONS,PUT,PATCH,DELETE"
        );
        ctx.set(
          "Access-Control-Allow-Headers",
          "Origin, X-Requested-With, Content-Type, Accept, Authorization, x-access-token"
        );

        await next();
      };
    }
  }

  @controller()
  class AuthController {
    @middleware("cors")
    @post("/admin/login")
    @validate({
      user_id: {
        type: "string",
        required: true,
      },
    })
    public async login(ctx: DarukContext, next: Next) {
      ctx.body = ctx.request.body.user_id;
    }
  }

  await myapp.binding();
  myapp.listen(3000);
})();
curl -X POST -d "user_id=abc" http://localhost:3000/admin/login -v
Note: Unnecessary use of -X or --request, POST is already inferred.
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 3000 (#0)
> POST /admin/login HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.54.0
> Accept: */*
> Content-Length: 11
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 11 out of 11 bytes
< HTTP/1.1 200 OK
< X-Request-Id: 8974cef3-6157-496e-a562-d16c79d1fd38
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: GET,POST,OPTIONS,PUT,PATCH,DELETE
< Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization, x-access-token
< Content-Type: text/plain; charset=utf-8
< Content-Length: 3
< Date: Wed, 27 Oct 2021 08:48:14 GMT
< Connection: keep-alive
<
* Connection #0 to host localhost left intact
abc

@xiaojue
Copy link
Contributor

xiaojue commented Oct 27, 2021

@whit15 看可以不可以给个最小 demo,我这边没有复现。

@xiaojue xiaojue added the invalid This doesn't seem right label Oct 27, 2021
@xiaojue xiaojue closed this as completed Oct 27, 2021
@xiaojue
Copy link
Contributor

xiaojue commented Nov 2, 2021

@whit15 也可以看置顶 issues,加我个人微信,我拽你进群

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

2 participants