CodeGenieApp / CodeGenieApp/serverless-express

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

Open
#417 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
5.3k
Forks
674
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the serverlessExpress handler path and compare the AWS API Gateway console test event, where headers is null, with a normal browser request. Reproduce the failure using the provided index.js Express route and CDK API definition; done means the console test no longer raises the null-header error and the behavior is covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, express, javascript, node.js
Domain
api, backend, cloud
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.