How to add CORS to the Dart server

const corsHeaders = {‘Access-Control-Allow-Origin’: ‘*’,‘Access-Control-Allow-Methods’: ‘GET, POST, DELETE, OPTIONS’,‘Access-Control-Allow-Headers’: ‘*’,};

‘Access-Control-Allow-Headers’: ‘https://foo.example',
const corsHeaders = {‘Access-Control-Allow-Origin’: ‘*’,‘Access-Control-Allow-Methods’: ‘GET, POST, DELETE, OPTIONS’,‘Access-Control-Allow-Headers’: ‘*’,};shelf.Response _options(shelf.Request request) =>(request.method == ‘OPTIONS’) ? shelf.Response.ok(null, headers: corsHeaders) : null;shelf.Response _cors(shelf.Response response) => response.change(headers: corsHeaders);final _fixCORS = shelf.createMiddleware(requestHandler: _options, responseHandler: _cors);
final handler = const shelf.Pipeline().addMiddleware(_fixCORS).addMiddleware(shelf.logRequests()).addHandler(_echoRequest);var server = await io.serve(handler,_hostname,8800,);

We are a team of rock-star developers, which provides fully-flexible and powerful products 💥.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store