-
Notifications
You must be signed in to change notification settings - Fork 79
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
Labels
invalid
This doesn't seem right
Comments
收到 |
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 |
@whit15 看可以不可以给个最小 demo,我这边没有复现。 |
@whit15 也可以看置顶 issues,加我个人微信,我拽你进群 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
大佬好,我这边准备加一个中间件来处理跨域问题,发现中间件只有在app中使用middlewareOrder: ['cors']全局加载才会生效,而在Controller里使用@Middleware('cors')局部加载不生效,辛苦大佬核实处理一下。
中间件定义
Controller
The text was updated successfully, but these errors were encountered: