From a701efba2ddd2f175dfe26ccc6ef9ae2b54add39 Mon Sep 17 00:00:00 2001 From: William Moore Date: Thu, 19 Jan 2023 11:28:20 -0600 Subject: [PATCH] Update to add CORS exceptions --- package-lock.json | 54 +++++++++++++++++++++++++++++++++++++++++++++++ package.json | 2 ++ src/index.ts | 3 +++ 3 files changed, 59 insertions(+) diff --git a/package-lock.json b/package-lock.json index 1aa7652..084e055 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,7 @@ "license": "ISC", "dependencies": { "body-parser": "^1.20.1", + "cors": "^2.8.5", "date-fns": "^2.29.3", "date-fns-tz": "^1.3.7", "dotenv": "^16.0.3", @@ -19,6 +20,7 @@ }, "devDependencies": { "@types/body-parser": "^1.19.2", + "@types/cors": "^2.8.13", "@types/date-fns": "^2.6.0", "@types/express": "^4.17.15", "@types/node": "^18.11.18", @@ -1110,6 +1112,15 @@ "@types/node": "*" } }, + "node_modules/@types/cors": { + "version": "2.8.13", + "resolved": "https://registry.npmjs.org/@types/cors/-/cors-2.8.13.tgz", + "integrity": "sha512-RG8AStHlUiV5ysZQKq97copd2UmVYw3/pRMLefISZ3S1hK104Cwm7iLQ3fTKx+lsUH2CE8FlLaYeEA2LSeqYUA==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/date-fns": { "version": "2.6.0", "resolved": "https://registry.npmjs.org/@types/date-fns/-/date-fns-2.6.0.tgz", @@ -1341,6 +1352,18 @@ "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", "integrity": "sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ==" }, + "node_modules/cors": { + "version": "2.8.5", + "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz", + "integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==", + "dependencies": { + "object-assign": "^4", + "vary": "^1" + }, + "engines": { + "node": ">= 0.10" + } + }, "node_modules/data-uri-to-buffer": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-4.0.1.tgz", @@ -1859,6 +1882,14 @@ "url": "https://opencollective.com/node-fetch" } }, + "node_modules/object-assign": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", + "integrity": "sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==", + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/object-inspect": { "version": "1.12.3", "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.12.3.tgz", @@ -3131,6 +3162,15 @@ "@types/node": "*" } }, + "@types/cors": { + "version": "2.8.13", + "resolved": "https://registry.npmjs.org/@types/cors/-/cors-2.8.13.tgz", + "integrity": "sha512-RG8AStHlUiV5ysZQKq97copd2UmVYw3/pRMLefISZ3S1hK104Cwm7iLQ3fTKx+lsUH2CE8FlLaYeEA2LSeqYUA==", + "dev": true, + "requires": { + "@types/node": "*" + } + }, "@types/date-fns": { "version": "2.6.0", "resolved": "https://registry.npmjs.org/@types/date-fns/-/date-fns-2.6.0.tgz", @@ -3308,6 +3348,15 @@ "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", "integrity": "sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ==" }, + "cors": { + "version": "2.8.5", + "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz", + "integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==", + "requires": { + "object-assign": "^4", + "vary": "^1" + } + }, "data-uri-to-buffer": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-4.0.1.tgz", @@ -3662,6 +3711,11 @@ "formdata-polyfill": "^4.0.10" } }, + "object-assign": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", + "integrity": "sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==" + }, "object-inspect": { "version": "1.12.3", "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.12.3.tgz", diff --git a/package.json b/package.json index 59a6b00..e6ebf47 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "license": "ISC", "dependencies": { "body-parser": "^1.20.1", + "cors": "^2.8.5", "date-fns": "^2.29.3", "date-fns-tz": "^1.3.7", "dotenv": "^16.0.3", @@ -25,6 +26,7 @@ }, "devDependencies": { "@types/body-parser": "^1.19.2", + "@types/cors": "^2.8.13", "@types/date-fns": "^2.6.0", "@types/express": "^4.17.15", "@types/node": "^18.11.18", diff --git a/src/index.ts b/src/index.ts index c6ef553..ececc85 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,6 +5,7 @@ import mongoose, { Schema } from 'mongoose'; import fetch from 'node-fetch'; import { subHours } from 'date-fns' import dateFnsTz from 'date-fns-tz'; +import cors from 'cors'; const { json } = bodyParser; const { formatInTimeZone } = dateFnsTz; @@ -32,6 +33,8 @@ const DataModel = mongoose.model('data', DataSchema); json(), ); + app.use(cors()); + app.get('/:name', async (req: any, res: any) => { const startTime = subHours(new Date(), 24); DataModel.find({