javascript - How to set CORS for two applications running as docker
问题描述
I'm running my frontend application (nextJS) at https://editor.website.com and my backend application (expressJS) at https://api.editor.website.com - both as docker container. I'm trying to upload some image files using graphQL and mostly it works, but sometimes it fails with these two errors:
POST https://api.editor.website.com/graphql 413
Access to fetch at 'https://api.editor.website.com/graphql' from origin 'https://editor.website.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
I tried to set universal cors to prevent this problem, but this doesn't work. So what am I missing?
Server
import express from 'express'
import cors from 'cors'
const app = express()
app.options('*', cors())
// app.use(cors())
server.applyMiddleware({ app, path: '/graphql' })
解决方案
CORS
如果它在大多数情况下都有效,我认为它没有关系(您可能会收到CORS
错误,因为如果另一个终止它,您不会点击链中的那个中间件)。据我所知,你收到了413 - Request Entity Too Large
。
我对 Apollo 服务器端没有太多经验,但您应该根据Apollo API Reference尝试以下操作:
server.applyMiddleware({
app, path: '/graphql',
cors: {
origin: 'https://editor.website.com',
optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
},
bodyParserConfig: {limit: '50mb'} // or your desired limit
})
编辑:
我们在聊天中继续讨论,发现服务前面有一个 nginx 反向代理,它有1MB
请求大小的限制。这解决了问题:https ://github.com/jwilder/nginx-proxy/issues/981#issuecomment-345434827
推荐阅读
- android - 将光标移动到 TextField 的末尾 - Nativescript
- jquery - 仅在数据表中导出列标题
- firebase - 为什么 iOS 平台可以在没有 google 服务的情况下使用 Firebase/Firestore?
- javascript - 我可以将分析脚本放在 javascript 文件中吗
- javascript - 如何通过在 svg 中开始一个新循环来停止以前的动画
- python - Mac 上的 Python Multiprocessing Pool.map() 以串行方式而不是并行方式运行进程
- linux-kernel - AMD VEGA64 在内核 > 4.15 上崩溃
- pmml - 如何在 nyoka 中添加自定义 Transformer/Estimator 并将管道转换为 PMML doc?
- r - 保留带有撇号和多大写名称的名称大小写(即麦当劳)
- asp.net-web-api - asp.net webapi:错误字符串无法将字符串转换为 http 响应消息