我有可与无服务器框架版本 1.25 配合使用的无服务器 API
出于安全原因,我想添加响应 header 。请帮助我如何通过 serverless.yml 文件设置以下标题。出于安全原因是否有必要添加此 header ?
• Content-Security-Policy:包括 default-src 'self'
• 严格传输安全最大年龄=31536000;包括子域;预加载
• X 内容类型选项:nosniff
• X-XSS-保护:1
• 缓存控制:max-age=0; Expires=-1 或 Expires: Fri, 01 Jan 1990 00:00:00 GMT;无缓存,必须重新验证
下面是我的无服务器应用serverless.yaml
service: myService
provider:
name: aws
runtime: nodejs6.10
stage: dev
region: eu-west-1
environment:
REGION: ${self:provider.region}
PROJECT_NAME: ${self:custom.projectName}
SERVERLESS_STAGE: ${self:provider.stage}
SERVERLESS_SERVICE: ${self:service}
IP_ADDRESS: http://example.com
functions:
getMyFunction:
handler: handler.getMyFunction
timeout: 30
events:
- http:
method: get
path: api/getMyFunction/v1
integration: lambda
cors: true
authorizer:
name: authorizerFunc
identitySource: method.request.header.Token
authorizationType: AWS_IAM
最佳答案
您可以使用 Lambda Proxy Integration .根据文档,您需要创建一个函数,该函数将在有人访问您的 API 端点时运行。
举个例子:
module.exports.hello = function (event, context, callback) {
console.log(event); // Contains incoming request data (e.g., query params, headers and more)
const response = {
statusCode: 200,
headers: {
"x-custom-header": "My Header Value"
},
body: JSON.stringify({ "message": "Hello World!" })
};
callback(null, response);
};
在你的 serverless.yml 中
functions:
index:
handler: handler.hello
events:
- http: GET hello
关于javascript - 如何在 serverless.yml 中设置响应头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49274262/