Skip to content

Commit daf8447

Browse files
committed
feat(app): auto-cors
1 parent 9b07269 commit daf8447

File tree

5 files changed

+19
-8
lines changed

5 files changed

+19
-8
lines changed

.env

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ PORT=3000
22
HOST=127.0.0.1
33
SECRET=secret_key
44
PUBLIC_URL=https://course-vue.javascript.ru
5-
CORS_ORIGIN=*
65

76
# Admin key is used to protect some maintenance features; remove or set empty to disable
87
# ADMIN_KEY=admin_key

src/app.module.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Module } from '@nestjs/common';
1+
import { MiddlewareConsumer, Module } from '@nestjs/common';
22
import { MikroOrmModule } from '@mikro-orm/nestjs';
33
import { ScheduleModule } from '@nestjs/schedule';
44
import { ConfigModule, ConfigService } from '@nestjs/config';
@@ -10,6 +10,7 @@ import { ImagesModule } from './images/images.module';
1010
import { MaintenanceModule } from './maintenance/maintenance.module';
1111
import config from './mikro-orm.config';
1212
import { configuration } from './configuration';
13+
import { CorsMiddleware } from './common/middleware/cors.middleware';
1314

1415
@Module({
1516
imports: [
@@ -40,4 +41,8 @@ import { configuration } from './configuration';
4041
MaintenanceModule,
4142
],
4243
})
43-
export class AppModule {}
44+
export class AppModule {
45+
configure(consumer: MiddlewareConsumer) {
46+
consumer.apply(CorsMiddleware).forRoutes('*');
47+
}
48+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Injectable, NestMiddleware } from '@nestjs/common';
2+
import { Request, Response, NextFunction } from 'express';
3+
4+
@Injectable()
5+
export class CorsMiddleware implements NestMiddleware {
6+
use(request: Request, response: Response, next: NextFunction) {
7+
response.header('Access-Control-Allow-Origin', request.headers.origin);
8+
response.header('Access-Control-Allow-Credentials', 'true');
9+
next();
10+
}
11+
}

src/configuration.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ export const configuration = () => ({
22
port: parseInt(process.env.PORT, 10) ?? 3000,
33
host: process.env.HOST ?? '127.0.0.1',
44
publicUrl: process.env.PUBLIC_URL ?? 'https://course-vue.javascript.ru',
5-
corsOrigin: process.env.CORS_ORIGIN ?? '*',
65
secret: process.env.SECRET ?? 'secret',
76
adminKey: process.env.ADMIN_KEY,
87
dbRefreshCron: process.env.DB_REFRESH_CRON,

src/main.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,13 @@ import passport from 'passport';
77
import session from 'express-session';
88
import SQLiteStoreFactory from 'connect-sqlite3';
99
import { AppModule } from './app.module';
10+
import * as process from 'process';
1011

1112
async function bootstrap() {
1213
const app = await NestFactory.create<NestExpressApplication>(AppModule);
1314
const configService = app.get<ConfigService>(ConfigService);
1415

1516
app.setGlobalPrefix('api');
16-
app.enableCors({
17-
origin: configService.get('corsOrigin'),
18-
credentials: true,
19-
});
2017
app.set('trust proxy', 1);
2118
app.use(
2219
session({

0 commit comments

Comments
 (0)