I want to use AWS ApiGateway to serve a swagger-ui of an API from a lambda function using express. For this I want to use the endpoint GET /docs.
My Lambda function swaggerUiLambdaHandler looks like this:
import express from 'express';
import serverless from 'serverless-http';
import swaggerUI from 'swagger-ui-express';
const options = {
swaggerOptions: {
url: 'https://petstore.swagger.io/v2/swagger.json',
},
};
const app = express();
app.use('/v1/docs', swaggerUI.serve, swaggerUI.setup(undefined, options));
module.exports.handler = serverless(app);
In the cdk stack of the api, I added the following 2 endpoints for serving the index.html and the .js/.css resources:
const docs = myApi.root.addResource('docs');
docs.addMethod('GET', new apigw.LambdaIntegration(swaggerUiLambdaHandler));
const docsProxy = docs.addResource('{proxy+}');
docsProxy.addMethod('GET', new apigw.LambdaIntegration(swaggerUiLambdaHandler));
However, calling the endpoint with the browser, all network requests succeed but I get a white canvas and the error
Uncaught SyntaxError: Unexpected token '<'
Do you have any idea? I think it might be related to the content-type returned by api gateway, but I would appreciate some help.