首页 > 解决方案 > Cors pref-light 请求似乎不起作用nestjs

问题描述

我尝试启用nestjs cors但没有成功,这里有各种从未奏效的方法。即使在下面的配置之后,nestjs 似乎也没有响应任何 OPTION 请求。

尝试过的解决方案

https://github.com/nestjs/nest/issues/1134#issuecomment-435902027

https://github.com/nestjs/nest/issues/1134#issuecomment-435893861

https://github.com/nestjs/nest/issues/1134#issuecomment-477531712

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


async function bootstrap() {
  const app = await NestFactory.create(AppModule,{ cors: true });


  const options = {
    "origin":"http://localhost:3000",  // attempted "origin":["http://localhost"]
    "methods": "GET,HEAD,PUT,PATCH,POST,DELETE",
    "preflightContinue": false,
    "optionsSuccessStatus": 204,
    "credentials":true,
    "allowedHeaders": "Content-Type, Accept,Authorization",

  }
  /**
   * {
    origin: [
      /^(.*)/,
           ],
    methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
    preflightContinue: false,
    optionsSuccessStatus: 200,
    credentials: true,
    allowedHeaders:
      'Origin,X-Requested-With,Content-Type,Accept,Authorization,authorization,X-Forwarded-for',
  }
   */
  app.enableCors(options)
  app.use((req, res, next) => {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    res.header('Access-Control-Allow-Headers', 'Content-Type, Accept,Authorization');
    next();
  });
  await app.listen(3000);
}
bootstrap();```


----------
**package.json**

    ```
{
  "name": "moat-backend",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "private": true,
  "license": "UNLICENSED",
  "scripts": {
    "prebuild": "rimraf dist",
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "nest start",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug --watch",
    "start:prod": "node dist/main",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json"
  },
  "dependencies": {
    "@nestjs/common": "^7.0.0",
    "@nestjs/core": "^7.0.0",
    "@nestjs/platform-express": "^7.0.0",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^6.5.4",
    "@nestjs/bull": "^0.1.1",
    "@nestjs/jwt": "^7.0.0",
    "@nestjs/passport": "^7.0.0",
    "@nestjs/swagger": "^4.5.5",
    "@nestjs/testing": "^7.0.9",
    "@nestjs/typeorm": "^7.0.0",
    "@types/bcrypt": "^3.0.0",
    "@types/dotenv": "^8.2.0",
    "@types/email-templates": "^7.0.1",
    "@types/nodemailer": "^6.4.0",
    "@types/passport": "^1.0.3",
    "bcrypt": "^4.0.1",
    "bcryptjs": "^2.4.3",
    "bull": "^3.13.0",
    "class-transformer": "^0.2.3",
    "class-validator": "^0.12.2",
    "crypto-js": "^4.0.0",
    "dotenv": "^8.2.0",
    "ejs": "^3.1.2",
    "email-templates": "^7.0.4",
    "fs-extra": "^9.0.0",
    "hash-anything": "^1.2.2",
    "hat": "^0.0.3",
    "keygenerator": "^1.0.4",
    "mysql": "^2.18.1",
    "nodemailer": "^6.4.6",
    "nodemon": "^2.0.3",
    "passport": "^0.4.1",
    "passport-jwt": "^4.0.0",
    "passport-local": "^1.0.0",
    "pug": "^2.0.4",
    "sha256": "^0.2.0",
    "swagger-ui-express": "^4.1.4",
    "typeorm": "^0.2.24",
    "uuid": "^7.0.3"

  },
  "devDependencies": {
    "@nestjs/cli": "^7.0.0",
    "@nestjs/schematics": "^7.0.0",
    "@nestjs/testing": "^7.0.9",
    "@types/express": "^4.17.3",
    "@types/jest": "25.1.4",
    "@types/node": "^13.9.1",
    "@types/supertest": "^2.0.8",
    "@typescript-eslint/eslint-plugin": "^2.23.0",
    "@typescript-eslint/parser": "^2.23.0",
    "eslint": "^6.8.0",
    "eslint-config-prettier": "^6.10.0",
    "eslint-plugin-import": "^2.20.1",
    "jest": "^25.1.0",
    "prettier": "^1.19.1",
    "supertest": "^4.0.2",
    "ts-jest": "25.2.1",
    "ts-loader": "^6.2.1",
    "ts-node": "^8.6.2",
    "tsconfig-paths": "^3.9.0",
    "typescript": "^3.7.4"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".spec.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

浏览器测试

Firefox 64.0 chrome 版本 75.0.3770.90(官方构建)(64 位)

平台

Linux 薄荷 19

客观的

使用 React 使用 Nest Api 在此处输入图像描述

标签: nestjs

解决方案


我找到了让它工作的方法我使用了 cors 包并将其连接到嵌套应用程序

const app = await NestFactory.create(AppModule,{ cors: true });


  const options = {
    "origin":true,  // attempted "origin":["http://localhost"]
    "methods": "GET,HEAD,PUT,PATCH,POST,DELETE",
    "preflightContinue": false,
    "optionsSuccessStatus": 200,
    "credentials":true,
    "allowedHeaders": "Content-Type, Accept,Authorization",

  }
 app.use(cor(options))
  await app.listen(3000);
}


推荐阅读