Angel3 CORS middleware
CORS headers middleware for Angel3. Ported from Express CORS middleware to Angel3 framework.
Installation
In your pubspec.yaml:
dependencies:
angel3_cors: ^10.0.0
Usage
import 'package:angel3_cors/angel3_cors.dart';
import 'package:angel3_framework/angel3_framework.dart';
Future configureServer(Angel app) async {
// The default options allow CORS from any origin, without credentials.
// 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) => 'Hello']));
// Of course, you can configure CORS.
app.chain([
cors(
CorsOptions(
origin: ['https://example.org', RegExp(r'^https://[a-z]+\.example\.org$')],
allowedHeaders: ['Content-Type', 'Authorization'],
),
),
]).group('/api', (router) {
router.get('/version', (req, res) => 'v0');
});
}
See the example file for all the options.
Migrating from 9.x to 10.0.0
The breaking changes of each release are listed in the CHANGELOG. Those of 10.0.0 that may need changes in an app:
origin: falsedisables CORS, as documented, where it allowed every origin withAccess-Control-Allow-Origin: *.- Refused origins: a refused origin, or a request without one, gets no
Access-Control-Allow-Originheader, where it gotfalse; its preflight is still answered. - Preflights only:
corsanswers only preflight requests (OPTIONSwith anAccess-Control-Request-Methodheader). AnotherOPTIONSrequest, or one madeOPTIONSby a method override, goes on to the app's handlers, wherecorsanswered it withsuccessStatus. Access-Control-Allow-Headersis sent on preflights only.originmust be one of the typesCorsOptionsdescribes (aString,RegExp,bool,Listor predicate); another is anArgumentErrorof the constructor, where it allowed every origin.
Libraries
- angel3_cors
- Angel CORS middleware.