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();