CodeGenieApp / CodeGenieApp/serverless-express

Route path routes to null when rawPath is empty from API Gateway

Offen
#612 0 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
JavaScript
Sterne
5.3k
Forks
676
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

When using API Gateway (v2 payload) with a custom domain and base path mapping, for example `api.example.com/v1`, where api.example.com is the custom domain and v1 is the base path, API Gateway will route the request to the lambda regardless of a trailing forward slash. For example `api.example.com/v1` or `api.example.com/v1/` will route to the lambda.

The issue with this is that when the trailing forward slash is missing, the lambda payload will contain a raw path that is empty:
```js
{
version: '2.0',
routeKey: '$default',
rawPath: '',
rawQueryString: ''
...
}
```

When using an express app with the serverless-express library, this results in a 404 response from the express app and a body containing the text `Cannot GET null`.

I'm not sure if this is an issue with express or serverless-express, but a workaround could be to place a default value in `function getRequestValuesFromApiGatewayEvent({ event })` to prevent this from happening. For example, line 13 of `event-sources/aws/api-gateway-v2.js` could be changed from:
```js
const requestPathOrRawPath = requestPath || rawPath
```
**to:**
```js
const requestPathOrRawPath = requestPath || rawPath || '/'
```

It sees most logical that both `api.example.com/v1` or `api.example.com/v1/` route to the same location. In my case I have a health check route at '/':
```js
app.all('/', (req: Request, res: Response) => {
res.send({
// Health check stats
})
})
```

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Beginne in event-sources/aws/api-gateway-v2.js, insbesondere bei getRequestValuesFromApiGatewayEvent und der Zuweisung von requestPathOrRawPath. Prüfe, wie ein API-Gateway-v2-Ereignis mit einem leeren rawPath in den Express-Anfragepfad umgewandelt wird; fertig ist die Änderung, wenn Anfragen mit und ohne abschließenden Schrägstrich zum Root-Pfad aufgelöst werden, anstatt Cannot GET null zu erzeugen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
aws, express, javascript, nodejs
Bereich
api, backend
Issue-Typ
Bug
Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
52/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.