簡體   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

我有部署在 AWS 彈性 beanstalk 上的 Nodejs express 應用程序。 我可以毫無問題地訪問 APIS,但是當我嘗試訪問 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;

在設置 SSL 並向應用程序添加域之后,我能夠加載 swagger UI 文檔。 也許這可能會在將來對某人有所幫助,但我希望在添加域之前我知道該怎么做。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM