CodeGenieApp / CodeGenieApp/serverless-express

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

Aperta
#417 5 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
JavaScript
Stelle
5.3k
Fork
676
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Inizia dal percorso dell'handler serverlessExpress e confronta l'evento di test della console AWS API Gateway, in cui headers è null, con una normale richiesta del browser. Riproduci il problema usando la route Express index.js fornita e la definizione dell'API CDK; il lavoro è completato quando il test della console non genera più l'errore di intestazione null e il comportamento è coperto da un test di regressione.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
aws, express, javascript, node.js
Ambito
api, backend, cloud
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.