AMP CMORS 中间软件
AMP CORS 中间软件添加了 CORS 和
AMP CORS 文档 所有 CORS 信头
aMP运行时间启动的请求。 中间软件只添加这些标题,如果
__amp_source_origin 查询参数已存在。 所有其他请求保持不变.
安装
通过 :
npm install @ampproject/toolbox-cors --save
使用量
以下是使用 快递数字 :
const express = require('express');
const ampCors = require('@ampproject/toolbox-cors');
const app = express();
// That's it!
app.use(ampCors());
...
请注意,AMP CORS并不依赖于Express,而是基于Node的HTTP请求和 反应对象.
按来源过滤
您可以额外过滤源代码的请求 。 例如:
app.use(ampCors({
sourceOriginPattern: /https:\/\/ampbyexample\.com$/
}));
这将只允许请求使用 https://amp.dev 设置为源代码。 所有其他来源的请求
将收到 403 反应,
来源核查
默认情况下, AMP CMORS 中间软件将只允许 AMP Caches 提出的请求
https://cdn.ampproject.org/caches.json 页面存档备份,存于互联网档案馆。所有其他
来源将收到 403 反应。 要允许来自所有来源的请求, 请禁用此选项
通过 verifyOrigin 选项 :
app.use(ampCors({
verifyOrigin: false
}));
允许刻录
默认情况下, AMP CORS 中间软件将允许 日历模式 用于请求AMP CORS.
要禁用此选项, 设置 allowCredentials 改为 false。 。 。 .
app.use(ampCors({
allowCredentials: false
}));
// => will not set "Access-Control-Allow-Credentials", "true"
允许 AMP- 重定向到
默认情况下,AMP CORS 中间软件将允许通过 AMP - 直接到设置 enableAmpRedirectTo 改为 false。 。 。 .
app.use(ampCors({
enableAmpRedirectTo: false
}));
// Access-Control-Expose-Headers: AMP-Redirect-To
日志
对于调试请求,您可以通过 verbose 选项 :
app.use(ampCors({
verbose: false
}));
电子邮件模式
Gmail已经 aMP CORS的具体要求。您可以通过 email option数字 :
app.use(ampCors({
email: true
}));
说明: 网站的默认 AMP CMORS 模式与电子邮件 CORS 模式相兼容(不再支持只支持来源验证). 如果你想支持两者,默认启用电子邮件模式是安全的.
示例
见 表达式.js 样本执行。 将增加AMP CORS标题的两种情况:
- 如果
__amp_source_origin查询参数与AMP-SAME-ORIGIN页眉 :
$ curl --header "AMP-SAME-ORIGIN: true" -I "http://localhost:3000/items?__amp_source_origin=https://localhost:3000"
HTTP/1.1 200 OK
X-Powered-By: Express
Access-Control-Allow-Origin: https://localhost:3000
Content-Type: application/json; charset=utf-8
...
- 如果
__amp_source_origin查询参数与Origin页眉 :
$ curl --header "Origin: https://amp-dev.cdn.ampproject.org" -I "http://localhost:3000/items?__amp_source_origin=https://localhost:3000"
HTTP/1.1 200 OK
Access-Control-Allow-Origin: https://amp-dev.cdn.ampproject.org
Content-Type: application/json; charset=utf-8
...
在所有其他情况下,将不设置 CORS 标题 .
$ curl -I localhost:3000/items
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
...