简体   繁体   English

托管在 aws beanstalk 上的 Nodejs express api swagger-ui-express API 文档上的 Cors 问题

[英]Cors issues on Nodejs express api swagger-ui-express API documentation hosted on aws beanstalk

I have Nodejs express app that is deployed on AWS elastic beanstalk.我有部署在 AWS 弹性 beanstalk 上的 Nodejs express 应用程序。 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.我可以毫无问题地访问 APIS,但是当我尝试访问 swagger API 文档时,我得到了 cors 错误,并且页面永远加载,直到它什么都不显示。

I am handling cors in the application already using the cors npm package but it is not helping.我正在使用cors npm 包处理应用程序中的 cors,但它没有帮助。

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;

After setting up SSL and adding a domain to the application, I was able to load swagger UI documentation.在设置 SSL 并向应用程序添加域之后,我能够加载 swagger UI 文档。 Maybe this might help someone in the future, but I wish I knew how to do it before adding a domain.也许这可能会在将来对某人有所帮助,但我希望在添加域之前我知道该怎么做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM