Skip to content

Commit

Permalink
Merge pull request #536 from LeChatErrant/cors
Browse files Browse the repository at this point in the history
Add CORS middleware
  • Loading branch information
LeChatErrant committed Dec 16, 2021
2 parents 00cfd03 + b122752 commit c6cb3f4
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ export SESSION_SECRET=keyboard_cat
export PORT=8000
export MODE=local

export WHITELIST=http://localhost,https://front.lechaterrant.com

export DEFAULT_ADMIN_EMAIL=admin@api-template.com
export DEFAULT_ADMIN_PASSWORD=admin

Expand Down
35 changes: 35 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"class-transformer": "^0.5.1",
"class-validator": "^0.13.1",
"connect-redis": "^6.0.0",
"cors": "^2.8.5",
"env-var": "^7.1.1",
"express": "^4.17.1",
"express-async-handler": "^1.2.0",
Expand All @@ -64,6 +65,7 @@
"devDependencies": {
"@types/bcrypt": "^5.0.0",
"@types/connect-redis": "0.0.18",
"@types/cors": "^2.8.12",
"@types/express": "^4.17.13",
"@types/express-session": "^1.17.4",
"@types/http-errors": "^1.8.1",
Expand Down
5 changes: 5 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import express from 'express';
import helmet from 'helmet';
import cors from 'cors';

/* This import is only used for class-transformer side effects */
import 'reflect-metadata';

import router from './routes';
import { config } from './appConfig';
import session from './appSession';
import requestLogger from './middlewares/requestLogger';
import errorMiddleware from './middlewares/errorMiddleware';
Expand All @@ -21,6 +23,9 @@ app.use(session);
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(helmet());
app.use(cors({
origin: config.whitelist,
}));
app.use(meMiddleware);
app.use(requestLogger);

Expand Down
1 change: 1 addition & 0 deletions src/appConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export enum MODES {

export const config = {
port: env('PORT').asPortNumber(),
whitelist: env('WHITELIST').asArray(','),
sessionSecret: env('SESSION_SECRET').asString(),
mode: env('MODE').asEnum(Object.values(MODES)),
defaultAdminEmail: env('DEFAULT_ADMIN_EMAIL').asString(),
Expand Down

0 comments on commit c6cb3f4

Please sign in to comment.