nest學習:跨域,前綴路徑,網站安全

 

 

yarn add helmet csurf

 

import { NestFactory } from '@nestjs/core';
import { Logger, ValidationPipe } from '@nestjs/common';

import * as helmet from 'helmet';
import * as csurf from 'csurf';

import { AppModule } from './app.module';

const PORT = process.env.PORT || 8000;

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  // 路徑前綴:如:http://www.dmyxs.com/api/v1/user
  app.setGlobalPrefix('api/v1');

  //cors:跨域資源共享,方式一:允許跨站訪問
  app.enableCors();
  // 方式二:const app = await NestFactory.create(AppModule, { cors: true });

  //防止跨站腳本攻擊
  app.use(helmet());

  //CSRF保護:跨站點請求僞造
  app.use(csurf());
  
  await app.listen(PORT, () => {
    Logger.log(
      `服務已經啓動,接口請訪問:http://wwww.localhost:${PORT}${PREFIX}`,
    )
  });
}
bootstrap();

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章