NodeJS - 仅将OPTIONS请求发送到REST API,POST或GET不跟随(Cloudflare) [英] NodeJS - only OPTIONS request is send to the REST API, POST or GET doesn't follow (Cloudflare)
问题描述
当我的网站调用REST api(下面的代码)时,只有 OPTIONS
请求通过, POST
或 GET
请求不遵循。 OPTIONS
请求成功通过CORS白名单。未记录 POST
或 GET
请求,白名单也不会阻止该请求。奇怪的是,Google Recaptcha请求有效,所有第三个网站都有,除了我的。 api与网站运行在同一个域上,只是另一个端口。
When my website calls the REST api (code below) then only the OPTIONS
request goes through, the POST
or GET
request doesn't follow. The OPTIONS
request successfully passes the CORS whitelist. The POST
or GET
request isn't logged, it's also not blocked by the whitelist. The weird thing is that the Google Recaptcha request works, all the third websites does, except for mine. The api is running on the same domain as the website, just another port.
我的网站正在使用Cloudflare,Cloudflare不断更改收件请求的IP地址。我的网站在通过cloudflare代理后,他的ip地址是ipv6。他的IP网站被列为白名单数组中的ipv6地址。
My website is using Cloudflare, Cloudflare constantly changes the IP addresses of the incomming requests. My website his ip address is ipv6 after it went through the cloudflare proxy. The website his ip is listed as an ipv6 address in the whitelist array.
const whitelist = ["*all the ip addresses"];
var corsOptions = {
origin: (origin, callback) => {
if (whitelist.indexOf(origin) !== -1) {
callback(null, true)
} else {
callback('Not allowed by CORS');
}
}
};
const app = express();
mongoose.connect(*mongodb credentials*);
app.use((req, res, next) => {
req.headers.origin = req.headers['cf-connecting-ip'] || req.headers['x-forwarded-for'] || req.connection.remoteAddress;
next();
});
app.use(morgan('combined'));
app.use(cors(corsOptions));
app.use(bodyParser.json({type: '*/*', limit: '2mb'}));
app.use(bodyParser.urlencoded({limit: '2mb', extended: true}));
app.use(*routing*);
module.exports = app;
推荐答案
app.use仅用于注册中间件。
您需要为您使用的每种方法指定路由。 app.get,app.put,app.delete等等。
你也可以使用app.all获取所有方法
你可以在这里找到更多信息: https://expressjs.com/en/guide/routing.html
app.use is only for registering middlewares. you need to specify a routing for each method you are using. app.get, app.put, app.delete and etc. you can also use app.all for all methods You can find more information here : https://expressjs.com/en/guide/routing.html
这篇关于NodeJS - 仅将OPTIONS请求发送到REST API,POST或GET不跟随(Cloudflare)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!