nestjs - 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
客观的
解决方案
我找到了让它工作的方法我使用了 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);
}
推荐阅读
- ruby-on-rails - Rails NoMethodError 用于编辑数据
- unity3d - 如何在 Unity C# 中单击时从 firebase 云消息传递中打开特定场景
- python - 将烧瓶应用程序部署到 amazon linux 2 ec2 实例
- wordpress - 根据 Wordpress 中的用户名为新用户自动生成电子邮件
- python - 如何在 Python 中一次对多个图像执行 OCR,并将所有数据打印到 XLSX 文件中?
- c++ - 模板化的 Q_OBJECT 基类
- java - 如果联系人与电话通讯簿中的应用程序相关联,例如什么是应用程序、skyip,则显示您的应用程序图标
- reactjs - 如何在 Reactjs 中显示链接共享博客预览
- stripe-payments - 当客户从条带帐户的所有者那里收到钱时,必须使用哪个条带 API?
- ios - 在ios14上调试iPhone Safari浏览器页面时不显示样式列表?