angel3_cors 8.4.0 copy "angel3_cors: ^8.4.0" to clipboard
angel3_cors: ^8.4.0 copied to clipboard

Angel3 CORS middleware. Ported from expressjs/cors to Angel3 framework.

example/example.dart

import 'dart:async';
import 'package:angel3_cors/angel3_cors.dart';
import 'package:angel3_framework/angel3_framework.dart';

Future configureServer(Angel app) async {
  // The default options will allow CORS for any request.
  // Combined with `fallback`, you can enable CORS application-wide.
  app.fallback(cors());

  // You can also enable CORS for a single route.
  app.get(
    '/my_api',
    chain([
      cors(),
      (req, res) {
        // Request handling logic here...
      },
    ]),
  );

  // Likewise, you can apply CORS to a group.
  app.chain([cors()]).group('/api', (router) {
    router.get('/version', (req, res) => 'v0');
  });

  // Of course, you can configure CORS.
  // The following is just a subset of the available options;
  app.fallback(
    cors(
      CorsOptions(
        origin: 'https://pub.flutter-io.cn',
        successStatus: 200, // default 204
        allowedHeaders: ['POST'],
        preflightContinue: false, // default false
      ),
    ),
  );

  // You can specify the origin in different ways:
  CorsOptions(origin: 'https://pub.flutter-io.cn');
  CorsOptions(origin: ['https://example.org', 'http://foo.bar']);
  CorsOptions(origin: RegExp(r'^foo\.[^$]+'));
  CorsOptions(origin: (String s) => s.length == 4);

  // Lastly, you can dynamically configure CORS:
  app.fallback(
    dynamicCors((req, res) {
      return CorsOptions(
        origin: [
          req.headers!.value('origin') ?? 'https://pub.flutter-io.cn',
          RegExp(r'\.com$'),
        ],
      );
    }),
  );
}
3
likes
160
points
326
downloads

Publisher

verified publisherdukefirehawk.com

Weekly Downloads

Angel3 CORS middleware. Ported from expressjs/cors to Angel3 framework.

Homepage
Repository (GitHub)
View/report issues
Contributing

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

angel3_framework

More

Packages that depend on angel3_cors