Skip to content
This repository has been archived by the owner on Sep 19, 2023. It is now read-only.

Commit

Permalink
Merge pull request #29 from matrix-org/michaelkaye/add_cors_headers
Browse files Browse the repository at this point in the history
Add middleware and cors headers
  • Loading branch information
michaelkaye authored Sep 20, 2018
2 parents e436e6c + adad3c4 commit c4cfb0d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"dependencies": {
"express": "^4.16.3",
"cors": "^2.8.4",
"express-validation": "^1.0.2",
"joi": "^13.3.0",
"js-yaml": "^3.12.0",
Expand Down
9 changes: 9 additions & 0 deletions src/middlewares.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
**/

const express = require('express');
const cors = require('cors');
const consoleMiddleware = require('./console-middleware.js');
const ClientError = require('./client-error.js');
const JoiError = require('./joi-error.js');
Expand All @@ -31,6 +32,14 @@ async function attachMiddlewares(app, opts) {
// Add req.console for nicer formatted logs
app.use(consoleMiddleware);

const corsOptions = {
origin: '*',
methods: ['GET', 'PUT', 'POST', 'DELETE', 'OPTIONS'],
allowedHeaders: ['Origin', 'X-Requested-With', 'Content-Type', 'Accept',
'Authorization']
}
app.use(cors(corsOptions));

// Add express-provided JSON but give it it's own unique error handling instead
// of falling back on the generic one - handing these back to the client is OK.
app.use(express.json(), jsonErrorMiddleware);
Expand Down

0 comments on commit c4cfb0d

Please sign in to comment.