首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用生成的[nodejs]代码(SWAGGER)的CORS问题

使用生成的[nodejs]代码(SWAGGER)的CORS问题
EN

Stack Overflow用户
提问于 2017-05-09 17:06:44
回答 1查看 4.4K关注 0票数 4

我根据我的swagger规范生成了我的服务器代码(nodejs-server)。

问题是,当我尝试从UI (不同的域)访问API时,我得到了一个众所周知的错误,即CORS没有启用:

XMLHttpRequest cannot load http://127.0.0.1:10010/events. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.

我生成的index.js如下所示:

代码语言:javascript
复制
'use strict';

var fs = require('fs'),
    path = require('path'),
    http = require('http');

var app = require('connect')();
var swaggerTools = require('swagger-tools');
var jsyaml = require('js-yaml');
var serverPort = 10010;

// swaggerRouter configuration
var options = {
  swaggerUi: path.join(__dirname, '/swagger.json'),
  controllers: path.join(__dirname, './controllers'),
  useStubs: process.env.NODE_ENV === 'development' // Conditionally turn on stubs (mock mode)
};

// The Swagger document (require it, build it programmatically, fetch it from a URL, ...)
var spec = fs.readFileSync(path.join(__dirname,'api/swagger.yaml'), 'utf8');
var swaggerDoc = jsyaml.safeLoad(spec);

// Initialize the Swagger middleware
swaggerTools.initializeMiddleware(swaggerDoc, function (middleware) {

  // Interpret Swagger resources and attach metadata to request - must be first in swagger-tools middleware chain
  app.use(middleware.swaggerMetadata());

  // Validate Swagger requests
  app.use(middleware.swaggerValidator());

  // Route validated requests to appropriate controller
  app.use(middleware.swaggerRouter(options));

  // Serve the Swagger documents and Swagger UI
  app.use(middleware.swaggerUi());

  // Start the server
  http.createServer(app).listen(serverPort, function () {
    console.log('Your server is listening on port %d (http://localhost:%d)', serverPort, serverPort);
    console.log('Swagger-ui is available on http://localhost:%d/docs', serverPort);
  });

});

不确定如何使用生成的代码启用CORS。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-17 08:55:34

在“初始化Swagger中间件”上面添加此代码,将域更改为正确的域。节点代码来自jvandemo's对相同问题No 'Access-Control-Allow-Origin' - Node / Apache Port Issue的回答

代码语言:javascript
复制
// Add headers
app.use(function (req, res, next) {

// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', 'http://server.to.allow.access.from');

// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');

// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');

// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);

// Pass to next layer of middleware
next();
});

swagger-codegen不会覆盖现有的index.js。

代码语言:javascript
复制
[main] INFO io.swagger.codegen.DefaultCodegen - 
Skipped overwriting index.js as the file already exists in 
C:\path\to\swagger-codegen\generated\nodejs\index.js
票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43875907

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档