Nodejs上的CORS问题Express API Swagger-UI-Express API文档托管在AWS Beanstalk上
我有部署在AWS弹性beanstalk上的Nodejs Express应用程序。我可以毫无问题地访问API,但是当我尝试访问Swagger API文档时,我会遇到CORS错误,并且页面将永远加载,直到什么都没有显示为止。
我正在使用cors
npm软件包中的应用程序中的CORS,但没有帮助。
有人知道我如何解决这个问题吗?我很乐意为任何想要提供帮助的人提供更多详细信息。
谢谢。
import router from '@api';
import * as statusCodes from '@constants/statusCode';
import { dbConnect } from '@dbConfig';
import swaggerDocument from '@swaggerDocs';
import { errors } from 'celebrate';
import compression from 'compression';
import cors from 'cors';
import dotenv from 'dotenv';
import express from 'express';
import helmet from 'helmet';
import createError from 'http-errors';
import logger from 'morgan';
import swaggerUi from 'swagger-ui-express';
dotenv.config();
dbConnect();
const app = express();
app.use(cors());
app.use(helmet());
app.use(compression());
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use('/api/v1', router);
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
// catch 404 errors
app.use((_, _1, next) => {
next(createError(statusCodes.HTTP_NOT_FOUND));
});
app.use((err, req, res, next) => {
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
res.status(err.status || statusCodes.HTTP_SERVER_ERROR);
const response = { message: err.message, error: err.status };
res.send(response);
next();
});
app.use(errors());
export default app;
I have Nodejs express app that is deployed on AWS elastic beanstalk. I can access the APIS without any problem but when I try to access swagger API docs, I get cors errors, and the page loads forever until it shows nothing.
I am handling cors in the application already using the cors
npm package but it is not helping.
Does anyone have an idea about how I can solve this? I would be happy to provide more details for anyone who wants to help.
Thanks.
import router from '@api';
import * as statusCodes from '@constants/statusCode';
import { dbConnect } from '@dbConfig';
import swaggerDocument from '@swaggerDocs';
import { errors } from 'celebrate';
import compression from 'compression';
import cors from 'cors';
import dotenv from 'dotenv';
import express from 'express';
import helmet from 'helmet';
import createError from 'http-errors';
import logger from 'morgan';
import swaggerUi from 'swagger-ui-express';
dotenv.config();
dbConnect();
const app = express();
app.use(cors());
app.use(helmet());
app.use(compression());
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use('/api/v1', router);
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
// catch 404 errors
app.use((_, _1, next) => {
next(createError(statusCodes.HTTP_NOT_FOUND));
});
app.use((err, req, res, next) => {
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
res.status(err.status || statusCodes.HTTP_SERVER_ERROR);
const response = { message: err.message, error: err.status };
res.send(response);
next();
});
app.use(errors());
export default app;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
设置SSL并在应用程序中添加域后,我能够加载Swagger UI文档。也许这可能在将来对某人有所帮助,但我希望我在添加域之前知道该怎么做。
After setting up SSL and adding a domain to the application, I was able to load swagger UI documentation. Maybe this might help someone in the future, but I wish I knew how to do it before adding a domain.