-
-
Notifications
You must be signed in to change notification settings - Fork 5
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
Middleware seems to work differently on Dart vs. Node #34
Comments
Hi @chimon2000 , I cannot reproduce this with the code below on nitric start or deployed to AWS, nothing is blocked for me. Can you try with the example code below? I am using: import 'package:nitric_sdk/nitric.dart';
Future<HttpContext> corsMiddleware(HttpContext ctx) {
ctx.res.headers['Access-Control-Allow-Origin'] = ['*'];
ctx.res.headers['Access-Control-Allow-Headers'] = [
'Origin, Content-Type, Accept, Authorization',
];
ctx.res.headers['Access-Control-Allow-Methods'] = ['GET,POST,DELETE,OPTIONS'];
return ctx.next();
}
void main() {
final helloApi = Nitric.api("main",
opts: ApiOptions(middlewares: List.from([corsMiddleware])));
helloApi.get("/hello/:name", (ctx) async {
final name = ctx.req.pathParams["name"]!;
ctx.res.body = "Hello $name";
return ctx.next();
});
} |
@davemooreuws I was able to narrow down the error. It occurs when I add the authorization header. Here's a very small example: |
Thanks for the example @chimon2000 , I'll take a look. |
Hi @chimon2000, This is simply the way CORs works with an Authorization header. It requires a successful "preflight request". Like so: api.options("/hello/:name", (ctx) async {
return ctx.next();
}); My forked repo with the changes: https://github.com/davemooreuws/nitric_middleware More information on CORs: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#simple_requests We do have an open feature request for CORs helpers: nitrictech/nitric#520 (comment) |
Closing, middleware checked for both node and dart. No issues. |
Bug Report
Issue
It seems like middleware works differently depending on the SDK used. When using the Node version, I can add a middleware to enable cors for the entire API like so:
If I apply that same version in Dart, the middleware fails to work as expected.
Steps
Steps to reproduce the behavior:
Expected
The middleware should function the same regardless of the environment.
Environment and setup information
N/A
Other info
N/A
The text was updated successfully, but these errors were encountered: