From b33cc21dad171720a0815b6222c0c63507c4d4d8 Mon Sep 17 00:00:00 2001 From: garrettmills Date: Tue, 20 Oct 2020 15:16:34 -0500 Subject: [PATCH] Apply cors before routes --- Units.flitter.js | 3 ++- app/CorsUnit.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ app/MiscUnit.js | 2 -- 3 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 app/CorsUnit.js diff --git a/Units.flitter.js b/Units.flitter.js index fd54de2..d3e7629 100644 --- a/Units.flitter.js +++ b/Units.flitter.js @@ -30,6 +30,7 @@ const FlitterUnits = { * available to the middleware-routing-controller stack. */ 'Upload' : new (require('flitter-upload/UploadUnit'))(), + 'CORS' : new (require('./app/CorsUnit'))(), /* * The Core Flitter Units @@ -94,4 +95,4 @@ const FlitterUnits = { 'App' : new (require('libflitter/app/AppUnit'))(), } -module.exports = exports = FlitterUnits \ No newline at end of file +module.exports = exports = FlitterUnits diff --git a/app/CorsUnit.js b/app/CorsUnit.js new file mode 100644 index 0000000..3e9bf36 --- /dev/null +++ b/app/CorsUnit.js @@ -0,0 +1,45 @@ +const Unit = require('libflitter/Unit') +const cors = require('cors') + +/* + * The Miscellaneous Unit + * ------------------------------------------------------------- + * This is a Unit file where you should make any modifications that + * your application may need such as custom Express add-ons. Changes + * here are loaded after the core Flitter units, but before the other + * units (secondary, custom, error, and App), so they are available to + * other units in the stack. + */ +class CorsUnit extends Unit { + + /* + * Initializes the unit class. + * This is called OUTSIDE of a Flitter context, + * so no other contexts are available. Modifications here + * take place before any part of Flitter is loaded or available. + */ + constructor(){ + super() + + + } + + /* + * Initialize the actual Unit. + * This is where most of the changes will go. + * This is provided an instance of the Express app + * so any custom changes can be made. The core Flitter + * contexts are available here, so things like config(), + * model(), etc. work. + */ + async go(app, context){ + app.express.use(cors()); + } + + name(){ + return "cors" + } + +} + +module.exports = exports = CorsUnit diff --git a/app/MiscUnit.js b/app/MiscUnit.js index a2fae20..c58db0b 100644 --- a/app/MiscUnit.js +++ b/app/MiscUnit.js @@ -33,8 +33,6 @@ class MiscUnit extends Unit { * model(), etc. work. */ async go(app, context){ - app.express.use(cors()); - String.prototype.capitalize = function(){ return this.charAt(0).toUpperCase() + this.slice(1) }