Hi,
I am using aws-serverless-express for exposing the express router endpoints. The endpoints work when the generated API gateway is used as it is. e.g https://711111abcde.execute-api.ap-southeast-2.amazonaws.com/dev/health.
But after mapping this hostname to a custom domain (https://api.myapp.com), the endpoints do not work. The same custom domain endpoints work when the lambdas are not in express app.
My Code:
const app = express();
app.use(cors())
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(awsServerlessExpressMiddleware.eventContext())
app.get('/health', (req, res) => {
console.log('APP health1: ');
res.status(200).json({ status: 'Next() API Health is OK and now ready to process requests...' });
});
The custom domain gives the headers as "x-powered-by' : express. This says that my requests hit the express app but somehow never routed to any of the endpoints configured.
const awsServerlessExpress = require('aws-serverless-express');
const route = require('./route');
const server = awsServerlessExpress.createServer(route);
exports.handler = (event, context) => awsServerlessExpress.proxy(server, event, context);
Are there any more configuration needed for custom domain (API Gateway). Please advise
But after mapping this hostname to a custom domain (https://api.myapp.com), the endpoints do not work.
The custom domain gives the headers as "x-powered-by' : express.
I see those as conflicting statements. What do you mean with "do not work"? Are you receiving 500? 400? 404? Do you see anything in the logs?
Hi @villasv
It gives me 404 error. Cannot find GET /v1/health. Here v1 is the base path mapping set in the API Gateway and /health is my endpoint configured in express.
There is nothing in the logs related to the express app. I added a simple middleware to debug the issue:
app.use((req, res, next) => {
console.log('Express app middleware... ');
next();
});
The above log printed. But the logs in the application endpoints(/health) never print. And in the client/postman, I get the 404 - Cannot GET /v1/health error
What I mean by "do not work" is:
https://711111abcde.execute-api.ap-southeast-2.amazonaws.com/dev/health - _Works with 200 OK and gives back the response message "Next() API Health is OK and now ready to process requests..."_
https://api.myapp.com/v1/health - _Does not work with 404 error. Cannot GET /v1/health_
It's been a while since I tested with a custom domain. What does event.path look like? I remember something along the lines of the custom domain mapping path being included in the event.path from API Gateway (which isn't ideal). If this is the case, you'll need to either strip that out from the path before calling the proxy method or change your Express/app layer to attach to /v1 instead of / (check out defining routes using router instead of app and attaching your router to app like I do here https://github.com/awslabs/aws-serverless-express/pull/155/files#diff-e55bc98107f12615bba2a6649f130228R94).
I might be remembering wrong though. Let me know and I'll dive deeper. If this does turn out to be the issue, I'd greatly appreciate a contribution to the Readme 馃檹
This is also discussed in #86
Hi @brettstack
#86 proved to be the solution for this. I ended up using https://www.npmjs.com/package/@turinggroup/serverless-express-custom-domain-middleware. npm package that was shared and was able to resolve this.
As you doubted, it had to to do with the event.path mapping from APIGateway.
Yes, I have defined all the routes using router only in the actual application but for the sake of brevity and debugging the issues, stripped out everything except defining a single route on the app itself.
I would definitely add this info to the README. It almost wasted my weeks effort looking for solution. I was almost about to give up using aws-serverless-express and move to the serverless-http library or otherwise remove express from the picture itself and go with serverless framework endpoint mappings.
Hope there is a plan to add this solution to the core aws-serverless-express library itself.
@muswain Can you go into a bit more detail on how you made this work with the npm package? New to this, so a bit confused
@Mohan1996 - You can do the following:
@turinggroup/serverless-express-custom-domain-middleware npm packageconst customDomainReroute = require('@turinggroup/serverless-express-custom-domain-middleware').customDomainRerouteconst app = express();app.use(customDomainReroute)Rest of the code is as it is used in any express route js. The above npm package helps in resolving the basePath that has been setup in API GW for a custom API domain (e.g https://api.myapp.com)
Closing in favor of #86
Most helpful comment
@Mohan1996 - You can do the following:
@turinggroup/serverless-express-custom-domain-middlewarenpm packageconst customDomainReroute = require('@turinggroup/serverless-express-custom-domain-middleware').customDomainRerouteconst app = express();app.use(customDomainReroute)Rest of the code is as it is used in any express route js. The above npm package helps in resolving the basePath that has been setup in API GW for a custom API domain (e.g https://api.myapp.com)