CodeGenieApp / CodeGenieApp/serverless-express

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

オープン
#417 コメント 5 件 リアクション 0 件 担当者 0 名 GitHub で見る
主要言語
JavaScript
スター
5.3k
フォーク
676
PR マージ指標
30日以内にマージされた PR はありません

説明

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"
}
```

コントリビューションガイド

コントリビューションガイドを開く

調査の方向性

serverlessExpress のハンドラーパスから開始し、headers が null になる AWS API Gateway コンソールのテストイベントと、通常のブラウザーリクエストを比較します。提供されている index.js の Express ルートと CDK API 定義を使って失敗を再現します。コンソールテストで null ヘッダーエラーが発生しなくなり、その動作が回帰テストでカバーされれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
aws, express, javascript, node.js
領域
api, backend, cloud
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。