NodeJS “无法读取未定义的属性'headers'

zd287kbt  于 2023-03-22  发布在  Node.js
关注(0)|答案(2)|浏览(262)

我使用下面的代码在Lambda Edge中设置会话头作为响应。但我总是得到这个错误。

{
    "errorType": "TypeError",
    "errorMessage": "Cannot read property 'headers' of undefined",
    "stack": [
        "TypeError: Cannot read property 'headers' of undefined",
        "    at Runtime.exports.handler (/var/task/index.js:118:30)",
        "    at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"
    ]
}

我使用的代码是这样的

exports.handler = function (event, config, callback) {
    const request = event.Records[0].cf.request;
    console.log(event.Records[0].cf);
    const response = event.Records[0].cf.response;
    const headers = response.headers;

    getConfigCached(request, function (err, config) {
        if (err) {
            callback(err, null);
        }
        else if (request.uri !== "/code" && !redirectIfNotAuthenticated(config, request, callback)) {
            callback(null, request);
        }
        else if (request.uri == "/code") {
            console.log('INSIDE CODE');
            let access_token = accessTokenCallback(request, callback);
            console.log(access_token);
            headers['session-token'] = [{ key: 'Session-Token', value: access_token }];
            callback(null, response);
        }
    });
};

CloudFront触发器设置为查看器请求

x9ybnkn6

x9ybnkn61#

请检查您在创建Lambda@Edge时配置的设置。它应该设置为Response origin,以便您能够设置响应头。
顺便说一句,有一个创建这种Lambda@edge AWS蓝图的蓝图

相关问题