CodeGenieApp / CodeGenieApp/serverless-express

Method execution tests from AWS API Gateway console fail with internal error

Đang mở
#417 5 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
JavaScript
Star
5.3k
Fork
674
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

The AWS API Gateway console allows you to navigate to your resource, choose "Test" and send a test request to your Lambda. When I tried this, the Lambda was throwing an error:
```
TypeError: Cannot read property 'if-modified-since' of null
```

Yet the API worked fine when accessed from a browser.

Turning up the logging, I saw this in the logs:
```
DEBUG {
message: 'SERVERLESS_EXPRESS:PROXY',
event: '{\n' +
" resource: '/api/{resource+}',\n" +
" path: '/api/test',\n" +
" httpMethod: 'GET',\n" +
' headers: null,\n' +
```

Yes, it looks like `headers` is null.

There are two workarounds:
- In the method execution form, define any random header in the Headers field.
- Wrap the `serverlessExpress` function to provide a default value for `headers`:

```
exports.handler = function handler(event, context, callback) {
return serverlessExpress({
app
})(
{
...event,
headers: event.headers || []
},
context,
callback
)
}
```

I know this is not a real-world issue, but I still wasted a fair amount of time trying to get to the bottom of it! I'm pretty sure I'm not doing anything odd in my code or deployment.

### index.js
```
import express from 'express'

const app = express()
const router = express.Router()

router.get('/api/test', (req, res) => {
res.json({ message: 'Hello World!' })
})
app.use('/', router)

export default app
```

### CDK
```
const backend = new Function(this, 'backend', {
functionName: 'test',
runtime: Runtime.NODEJS_14_X,
environment: {
AWS_NODEJS_CONNECTION_REUSE_ENABLED: '1'
},
handler: 'main.handler',
code: Code.fromAsset(path.join(path.dirname(require.resolve('test')), 'build'))
})

api.root.addResource('api').addResource('{resource+}').addMethod('ANY', new LambdaIntegration(backend))
```

### package.json
```
"dependencies": {
"@vendia/serverless-express": "^4.3.7",
"express": "^4.17.1"
}
```

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Hướng nghiên cứu

Bắt đầu từ đường dẫn handler serverlessExpress và so sánh sự kiện kiểm thử của bảng điều khiển AWS API Gateway, trong đó headers là null, với một yêu cầu trình duyệt thông thường. Tái hiện lỗi bằng route Express index.js được cung cấp và định nghĩa API CDK; hoàn tất khi bài kiểm thử trên bảng điều khiển không còn phát sinh lỗi header null và hành vi này được bao phủ bởi một bài kiểm thử hồi quy.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
aws, express, javascript, node.js
Lĩnh vực
api, backend, cloud
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.