Nodejs上的CORS问题Express API Swagger-UI-Express API文档托管在AWS Beanstalk上

发布于 2025-02-11 07:18:13 字数 1736 浏览 3 评论 0原文

我有部署在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.

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贩梦商人 2025-02-18 07:18:13

设置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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文