File tree Expand file tree Collapse file tree 5 files changed +19
-8
lines changed
Expand file tree Collapse file tree 5 files changed +19
-8
lines changed Original file line number Diff line number Diff line change @@ -2,7 +2,6 @@ PORT=3000
22HOST = 127.0.0.1
33SECRET = secret_key
44PUBLIC_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
Original file line number Diff line number Diff line change 1- import { Module } from '@nestjs/common' ;
1+ import { MiddlewareConsumer , Module } from '@nestjs/common' ;
22import { MikroOrmModule } from '@mikro-orm/nestjs' ;
33import { ScheduleModule } from '@nestjs/schedule' ;
44import { ConfigModule , ConfigService } from '@nestjs/config' ;
@@ -10,6 +10,7 @@ import { ImagesModule } from './images/images.module';
1010import { MaintenanceModule } from './maintenance/maintenance.module' ;
1111import config from './mikro-orm.config' ;
1212import { 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff 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 ,
Original file line number Diff line number Diff line change @@ -7,16 +7,13 @@ import passport from 'passport';
77import session from 'express-session' ;
88import SQLiteStoreFactory from 'connect-sqlite3' ;
99import { AppModule } from './app.module' ;
10+ import * as process from 'process' ;
1011
1112async 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 ( {
You can’t perform that action at this time.
0 commit comments