CodeGenieApp / CodeGenieApp/serverless-express
Method execution tests from AWS API Gateway console fail with internal error
- Lenguaje dominante
- JavaScript
- Estrellas
- 5.3k
- Forks
- 676
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Descripción
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"
}
```
Guía de contribución
Línea de trabajo
Comienza en la ruta del handler de serverlessExpress y compara el evento de prueba de la consola de AWS API Gateway, donde headers es null, con una solicitud normal del navegador. Reproduce el fallo usando la ruta de Express de index.js proporcionada y la definición de API de CDK; se considera terminado cuando la prueba de la consola ya no provoca el error de encabezado nulo y el comportamiento está cubierto por una prueba de regresión.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- aws, express, javascript, node.js
- Área
- api, backend, cloud
- Tipo de issue
- Error
- Dificultad
- 3/5
- Tiempo estimado
- 1-2 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100