NodeJS 如果使用“preFlightContinue”,OPTIONS请求如何处理

kxeu7u2r  于 12个月前  发布在  Node.js
关注(0)|答案(1)|浏览(143)

我意识到我不完全理解Express是如何处理“preflight OPTIONS请求”的,这是一个标准设置:

const cors = require('cors');

const app = express();

app.use(cors({
  preflightContinue: true  // here
}));

我有两个问题:

  1. preflightContinue: truepreflightContinue: false有什么区别
    1.哪个是默认的快速设置?- 我猜标志默认为false
    1.最重要的是:对于preflightContinue: true,* 何时/如何 * 响应OPTIONS请求?它是否会神奇地响应正常的请求?
cetgtptt

cetgtptt1#

此选项允许您在设置了必要的头之后,使用自己的中间件来制造对preflight请求的响应。参见源代码。
一个(完全人为的)例子是在preflight响应中设置一个非标准的header,如下所示:

app.use(cors({preflightContinue: true}))
.options("*", function(req, res) {
  res.set("X-Preflight-Response", "true").end();
});

需要此选项的更好理由可以在https://github.com/expressjs/cors/issues/305#issuecomment-1761041281中找到。

相关问题