From 03474a422e1c3e1eab08ef6ca217dba1c3c1a6ee Mon Sep 17 00:00:00 2001 From: dragongoose <19649813+dragongoose@users.noreply.github.com> Date: Sun, 16 Apr 2023 16:09:07 -0400 Subject: [PATCH] Add better cors implementation --- index.ts | 9 ++------- util/cors.ts | 9 +++++++++ 2 files changed, 11 insertions(+), 7 deletions(-) create mode 100644 util/cors.ts diff --git a/index.ts b/index.ts index 8d0c8bb..77e00a5 100644 --- a/index.ts +++ b/index.ts @@ -5,19 +5,14 @@ import { errorHandler } from './util/errorHandler' import { wsServer } from './routes/proxyRoute'; import compression from 'compression' import { version } from './package.json'; +import { cors } from './util/cors'; dotenv.config() const app: Express = express(); const port = process.env.PORT -app.all('/', function(req, res, next) { - res.header("Access-Control-Allow-Origin", "*"); - res.header("Access-Control-Allow-Headers", "X-Requested-With"); - next(); - }); - - +app.use(cors()) app.use(compression({})) app.use(routes) diff --git a/util/cors.ts b/util/cors.ts new file mode 100644 index 0000000..c071199 --- /dev/null +++ b/util/cors.ts @@ -0,0 +1,9 @@ +import { NextFunction, Request, Response } from "express"; + +export const cors = () => { + return (req: Request, res: Response, next: NextFunction) => { + res.header("Access-Control-Allow-Origin", "*"); + res.header("Access-Control-Allow-Headers", "X-Requested-With"); + next(); + } +} \ No newline at end of file