【发布时间】:2019-11-14 01:12:16
【问题描述】:
我使用下面的代码在 express 中创建了一个简单的路线。
const express = require("express");
const app = express();
app.get('/route1', (req, res) => {
res.send({ data: "Route 1" });
});
app.listen(3000);
当我运行curl -X GET http://localhost:3000/route1 时,我得到{"data":"Route 1"} 作为响应。
但是,我尝试运行 curl -X OPTIONS http://localhost:3000/route1 来模拟 CORS 飞行前请求。我收到了GET,HEAD 作为回复。
我找不到任何支持这种行为的文档。为什么上面的路由响应OPTIONS请求?
(注意:我没有使用过任何其他软件包,例如CORS)
编辑
根据 Quentin 的回答,我尝试发出另一个带有相关标头并在 curl 中显示标头标志的 OPTIONS 请求。
curl -i -X OPTIONS http://localhost:3000/route1 \
-H 'Access-Control-Request-Method: POST' \
-H 'Access-Control-Request-Headers: content-type,x-requested-with'
HTTP/1.1 200 OK
X-Powered-By: Express
Allow: GET,HEAD
Content-Type: text/html; charset=utf-8
Content-Length: 8
ETag: W/"8-ZRAf8oNBS3Bjb/SU2GYZCmbtmXg"
Date: Wed, 03 Jul 2019 11:14:07 GMT
Connection: keep-alive
GET,HEAD
【问题讨论】:
标签: javascript node.js express preflight