question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

req.path is different between default API Gateway domain and custom domain

See original GitHub issue

According to this guide:

http://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-custom-domains.html#how-to-custom-domains-call-api-with-sni

setting base path mappings on a custom domain should produce the same result as the default API Gateway domain. However, this is the result I’m seeing with my router (using {proxy+}):

https://udxjef.execute-api.us-east-1.amazonaws.com/pro/foo => req.path is “/foo” https://apis.example.com/petstore/foo => req.path is “/petstore/foo”

Since these paths are different, a route will not be able to match both URLs at the same time.

In our case, we have been able to work around this by setting an environment variable in our Lambda function and then telling our routes that they are mounted to a base path:

app.get(process.env.API_PATH + '/foo', myRouteHandler)

We can then include the same base path on the default URL to get the equivalent route as would be seen by the custom domain:

https://udxjef.execute-api.us-east-1.amazonaws.com/pro/petstore/foo => req.path is “/petstore/foo” https://apis.example.com/petstore/foo => req.path is “/petstore/foo”

This works, but it seems like an unnecessary step and is inconsistent with the expectations outlined in the guide noted above.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:24
  • Comments:24

github_iconTop GitHub Comments

6reactions
glmouradcommented, Apr 15, 2020

Try to use this code as your handler:

// lambda.js
const awsServerlessExpress = require('aws-serverless-express')
const app = require('./app')
const server = awsServerlessExpress.createServer(app)

exports.handler = (event, context) => { 
    
    //Remove the stage
    if (event.requestContext && event.requestContext.stage) {
        event.path = event.path.replace('/' + event.requestContext.stage, '');
    }
   
    awsServerlessExpress.proxy(server, event, context) 
   
}
5reactions
charleswhchancommented, Jul 20, 2017

Any update for this? Really want to use aws-serverless-express without resorting to custom hacks. Thanks. 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

req.path is different between default API Gateway domain ...
It seems that pathParameter.proxy is consistent whether used with custom domains or not, so you can swap that in for the url path....
Read more >
Setting up custom domain names for REST APIs
Learn how to set up a custom domain name for a REST API in API Gateway. ... custom domain names, you can set...
Read more >
Custom Domain Name with AWS API Gateway - YouTube
In this video, I teach you how to clean up your API Gateway url from the default into a custom url such as...
Read more >
Setting up custom domain names for HTTP APIs - 亚马逊云科技
Learn how to set up a custom domain name for an HTTP API in API Gateway. ... custom domain names, you can set...
Read more >
Configure custom domain name for Azure API Management ...
Gateway, Default is: <apim-service-name>.azure-api.net . Gateway is the only endpoint available for configuration in the Consumption tier. The ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found