swagger-editor
swagger-editor copied to clipboard
Unable to enable CORS in server generated nodejs from Swagger-Editor
Q&A (please complete the following information)
- OS: Windows 10
- Browser: Chrome
- Method of installation: npm
- Swagger/OpenAPI version: OpenAPI 3.0
Content & configuration
OpenAPI definition:
openapi: 3.0.1
info:
title: Test APIs
version: 1.0.0
servers:
- url: http://localhost:8080/api/v1
tags:
- name: Company
paths:
/companies:
get:
tags:
- Company
summary: Get all companies
description: Returns all companies
operationId: getCompanies
responses:
200:
description: success
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Company'
components:
schemas:
Company:
type: object
required:
- name
properties:
id:
type: integer
readOnly: true
name:
type: string
isDeleted:
type: boolean
Describe the bug you're encountering
Hi, I generated the server code (nodejs-server) using the above yaml from Swagger Editor and was encountering CORS errors when I tried to call the api from an angular client.
I've installed cors package (https://www.npmjs.com/package/cors) and added the following in the index.js file and still encountering the same error.
I've also tried using other methods (as shown below) but still getting the same error: app.use(function (req, res, next) { res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE'); res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type'); res.setHeader('Access-Control-Allow-Credentials', true); next(); });
Any idea on how to resolve this?
Found a workaround in https://github.com/bug-hunters/oas3-tools/issues/19
The issue is still relevant