From e64cbf6a3db82425b371f9fcb8341412a2257c9d Mon Sep 17 00:00:00 2001 From: Richie Bendall Date: Wed, 18 Mar 2020 18:21:30 +1300 Subject: [PATCH] refactor: Use external cors implementation Signed-off-by: Richie Bendall --- middleware/cors.js | 8 -------- package.json | 1 + routes/post.js | 3 +++ server.js | 2 +- yarn.lock | Bin 184394 -> 184719 bytes 5 files changed, 5 insertions(+), 9 deletions(-) delete mode 100644 middleware/cors.js diff --git a/middleware/cors.js b/middleware/cors.js deleted file mode 100644 index 58910309..00000000 --- a/middleware/cors.js +++ /dev/null @@ -1,8 +0,0 @@ -module.exports = (_req, res, next) => { - res.header('Access-Control-Allow-Origin', '*') - res.header( - 'Access-Control-Allow-Headers', - 'Origin, X-Requested-With, Content-Type, Accept' - ) - next() -} diff --git a/package.json b/package.json index d317b635..9fdf82b6 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "@sindresorhus/is": "^2.0.0", "any-size": "^1.0.0", "btoa": "^1.2.1", + "cors": "^2.8.5", "ejs": "^3.0.1", "escape-goat": "^3.0.0", "express": "^4.17.1", diff --git a/routes/post.js b/routes/post.js index 8930a3e6..613c61c0 100644 --- a/routes/post.js +++ b/routes/post.js @@ -17,8 +17,10 @@ const { validDomainId } = require('./utils') function getUserData ({ query, body }) { // If query parameters provided if (size(query) > 0) return query + // If the data parsed as {'{data: "value"}': ''} if (size(body) === 1 && !Object.values(body)[0]) return JSON.parse(Object.keys(body)[0]) + // Fallback return body } @@ -26,6 +28,7 @@ function getUserData ({ query, body }) { // HTTP POST API module.exports = async (req, res) => { const { hostname } = req + // Get different parts of hostname (example: remy.mit-license.org -> ['remy', 'mit-license', 'org']) const params = hostname.split('.') diff --git a/server.js b/server.js index 4b682ad4..ab938e92 100644 --- a/server.js +++ b/server.js @@ -48,7 +48,7 @@ app.use( // Middleware // CORS -app.use(require('./middleware/cors')) +app.use(require('cors')) // Parse URL-encoded bodies (as sent by HTML forms) app.use( express.urlencoded({ diff --git a/yarn.lock b/yarn.lock index 9841028ba197644b4aeecccf0439d17557b426bf..03e771aae1f32a9bab50289fdb680871d80c8534 100644 GIT binary patch delta 245 zcmX@LfV+P&_lDAP&Z1(6I3qm^J=5u$DvY+a$@xXa`nvicN*64wSCU?(oSK+yXqb{{ zYG`U{l#*g$mSmY`V3uZ78hrx=P4+~nJ95706|$|Q6-RPsMK6o mzP+%VagF|T#pR5`?Q+W*x63VO+9tvhXQ-pF{rqmGY7qdl?N4n0 delta 38 wcmV+>0NMYKqYKKQ3$S%}gP(Y}pLhYNFNayE0k>JG0=E>CK$b(dT)hG_6j%rlVgLXD