加入下方語法即可讓API跨站請求,可參考檔案main.ts
app.enableCors();
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app/app.module';
async function bootstrap() {
// API設定
const app = await NestFactory.create(AppModule);
const globalPrefix = 'api';
app.setGlobalPrefix(globalPrefix);
const port = process.env.port || 3333;
// 允許API跨站請求
app.enableCors();
// 啟動並監聽API
await app.listen(port, () => {
console.log('Listening at http://localhost:' + port + '/' + globalPrefix);
});
}
bootstrap();