From cfd78b1658601d36ab8e9cbb7c0484a4676dff49 Mon Sep 17 00:00:00 2001 From: AnthonyLzq Date: Sun, 6 Mar 2022 20:48:09 -0500 Subject: [PATCH] feat: added cors to express --- example/express/package.json | 2 ++ example/express/src/network/server.ts | 3 ++- example/express/yarn.lock | 20 +++++++++++++++++++- lib/src/functions/api.js | 3 ++- lib/src/index.js | 4 ++-- 5 files changed, 27 insertions(+), 5 deletions(-) diff --git a/example/express/package.json b/example/express/package.json index fa51a54..8bc827e 100644 --- a/example/express/package.json +++ b/example/express/package.json @@ -16,6 +16,7 @@ "dependencies": { "@sinclair/typebox": "^0.23.4", "ajv": "^6", + "cors": "^2.8.5", "express": "^4.17.3", "http-errors": "^2.0.0", "mongoose": "^6.2.4", @@ -23,6 +24,7 @@ "swagger-ui-express": "^4.3.0" }, "devDependencies": { + "@types/cors": "^2.8.12", "@types/express": "^4.17.13", "@types/http-errors": "^1.8.2", "@types/morgan": "^1.9.3", diff --git a/example/express/src/network/server.ts b/example/express/src/network/server.ts index fc56ca1..c35ae6b 100644 --- a/example/express/src/network/server.ts +++ b/example/express/src/network/server.ts @@ -1,6 +1,7 @@ import express from 'express' import mongoose from 'mongoose' import morgan from 'morgan' +import cors from 'cors' import { applyRoutes } from './router' @@ -16,7 +17,7 @@ class Server { } private _config() { - this._app.set('port', PORT) + this._app.use(cors()) this._app.use(morgan('dev')) this._app.use(express.json()) this._app.use(express.urlencoded({ extended: false })) diff --git a/example/express/yarn.lock b/example/express/yarn.lock index f7f54a6..2b8448c 100644 --- a/example/express/yarn.lock +++ b/example/express/yarn.lock @@ -147,6 +147,11 @@ dependencies: "@types/node" "*" +"@types/cors@^2.8.12": + version "2.8.12" + resolved "https://registry.yarnpkg.com/@types/cors/-/cors-2.8.12.tgz#6b2c510a7ad7039e98e7b8d3d6598f4359e5c080" + integrity sha512-vt+kDhq/M2ayberEtJcIN/hxXy1Pk+59g2FV/ZQceeaTyCtCucjL2Q7FXlFjtWn4n15KCr1NE2lNNFhp0lEThw== + "@types/eslint-scope@^3.7.3": version "3.7.3" resolved "https://registry.yarnpkg.com/@types/eslint-scope/-/eslint-scope-3.7.3.tgz#125b88504b61e3c8bc6f870882003253005c3224" @@ -1139,6 +1144,14 @@ core-util-is@~1.0.0: resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.3.tgz#a6042d3634c2b27e9328f837b965fac83808db85" integrity sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ== +cors@^2.8.5: + version "2.8.5" + resolved "https://registry.yarnpkg.com/cors/-/cors-2.8.5.tgz#eac11da51592dd86b9f06f6e7ac293b3df875d29" + integrity sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g== + dependencies: + object-assign "^4" + vary "^1" + create-require@^1.1.0: version "1.1.1" resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333" @@ -2873,6 +2886,11 @@ null-check@^1.0.0: resolved "https://registry.yarnpkg.com/null-check/-/null-check-1.0.0.tgz#977dffd7176012b9ec30d2a39db5cf72a0439edd" integrity sha1-l33/1xdgErnsMNKjnbXPcqBDnt0= +object-assign@^4: + version "4.1.1" + resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863" + integrity sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM= + object-inspect@^1.11.0, object-inspect@^1.9.0: version "1.12.0" resolved "https://registry.yarnpkg.com/object-inspect/-/object-inspect-1.12.0.tgz#6e2c120e868fd1fd18cb4f18c31741d0d6e776f0" @@ -4043,7 +4061,7 @@ validate-npm-package-license@^3.0.1: spdx-correct "^3.0.0" spdx-expression-parse "^3.0.0" -vary@~1.1.2: +vary@^1, vary@~1.1.2: version "1.1.2" resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc" integrity sha1-IpnwLG3tMNSllhsLn3RSShj2NPw= diff --git a/lib/src/functions/api.js b/lib/src/functions/api.js index 7283ebf..d0552bf 100644 --- a/lib/src/functions/api.js +++ b/lib/src/functions/api.js @@ -661,6 +661,7 @@ export { applyRoutes } content: `import express from 'express' import mongoose from 'mongoose' import morgan from 'morgan' +import cors from 'cors' import { applyRoutes } from './router' @@ -676,7 +677,7 @@ class Server { } private _config() { - this._app.set('port', PORT) + this._app.use(cors()) this._app.use(morgan('dev')) this._app.use(express.json()) this._app.use(express.urlencoded({ extended: false })) diff --git a/lib/src/index.js b/lib/src/index.js index a933e97..3e9d8ab 100644 --- a/lib/src/index.js +++ b/lib/src/index.js @@ -57,13 +57,13 @@ module.exports = async ({ cliProgress.Presets.shades_classic ) - const expressProdPackages = 'express morgan swagger-ui-express' + const expressProdPackages = 'express morgan swagger-ui-express cors' const fastifyProdPackages = 'fastify fastify-swagger' const prodPackages = `${manager} http-errors mongoose @sinclair/typebox ajv@^6 ${ fastify ? fastifyProdPackages : expressProdPackages }` - const expressDevPackages = `@types/express @types/morgan @types/swagger-ui-express` + const expressDevPackages = `@types/express @types/morgan @types/swagger-ui-express @types/cors` const fastifyDevPackages = '' const devPackages = `${manager} -D \ @types/http-errors \