Swagger - 更改项目目录

Posted

技术标签:

【中文标题】Swagger - 更改项目目录【英文标题】:Swagger - Change project directory 【发布时间】:2016-11-01 14:49:30 【问题描述】:

我在工作空间的根目录中创建了新项目 (nodeJS),过了一段时间我想将项目的客户端和服务器端分开。为此,我在项目的根目录中创建了两个文件夹(服务器和客户端),并将所有文件和目录移动到适当的文件夹中。

之后我无法启动服务器,因为它说 Swagger 找不到 swagger.yaml 文件(我将其移至服务器文件夹)

 Error: ENOENT: no such file or directory, open 'C:\Users\User\Documents\movieCollection\api\swagger\swagger.yaml'
    at Error (native)
    at Object.fs.openSync (fs.js:634:18)
    at Object.fs.readFileSync (fs.js:502:33)
    at C:\Users\User\AppData\Roaming\npm\node_modules\swagger\lib\commands\project\project.js:283:44
    at findProjectFile (C:\Users\User\AppData\Roaming\npm\node_modules\swagger\lib\commands\project\project.js:308:14)
    at readProject (C:\Users\User\AppData\Roaming\npm\node_modules\swagger\lib\commands\project\project.js:268:3)
    at Command.edit (C:\Users\User\AppData\Roaming\npm\node_modules\swagger\lib\commands\project\project.js:240:3)
    at Command.<anonymous> (C:\Users\User\AppData\Roaming\npm\node_modules\swagger\lib\util\cli.js:167:27)
    at Command.listener (C:\Users\User\AppData\Roaming\npm\node_modules\swagger\node_modules\commander\index.js:301:8)
    at emitTwo (events.js:106:13)
  errno: -4058,
  code: 'ENOENT',
  syscall: 'open',
  path: 'C:\\Users\\User\\Documents\\movieCollection\\api\\swagger\\swagger.yaml' 

我尝试在节点服务器的配置文件夹中编辑 default.yaml 文件,但问题仍然存在。

有没有办法将swagger项目配置指向新的laction(目录->服务器文件夹)?

default.yaml:

# swagger configuration file

# values in the swagger hash are system configuration for swagger-node
swagger:

  fittingsDirs: [ api/fittings ]
  defaultPipe: null
  swaggerControllerPipe: swagger_controllers  # defines the standard processing pipe for controllers

  # values defined in the bagpipes key are the bagpipes pipes and fittings definitions
  # (see https://github.com/apigee-127/bagpipes)
  bagpipes:
_router:
  name: swagger_router
  mockMode: false
  mockControllersDirs: [ ./server/api/mocks ]
  controllersDirs: [ ./server/api/controllers ]

_swagger_validate:
  name: swagger_validator
  validateResponse: true

# pipe for all swagger-node controllers
swagger_controllers:
  - onError: json_error_handler
  - cors
  - swagger_security
  - _swagger_validate
  - express_compatibility
  - _router

# pipe to serve swagger (endpoint is in swagger.yaml)
swagger_raw:
  name: swagger_raw

# any other values in this file are just loaded into the config for application access...

编辑:添加了 app.js

app.js:

'use strict';

var SwaggerExpress = require('swagger-express-mw');
var express = require("express");
var config = require('config');
var cors = require('cors');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var passport = require('passport');
var flash = require('connect-flash');
var session = require('express-session');
var routes = require('./src/routes');
var routingPath = '../server';
var app = express();

var spec = fs.readFileSync('../server/api/swagger.yaml', 'utf8');
var swaggerDoc = jsyaml.safeLoad(spec);
// Enable CORS
app.use(cors());

// Bootstrap routes
app.use(routes);

// Static files
app.use('/', express.static(__dirname + '/../public'));
module.exports = app; // for testing

var config = 
  appRoot: __dirname   // required config
;
swaggerExpress.runner.swagger.basePath = routingPath;

SwaggerExpress.create(config, function(err, swaggerExpress) 
  if (err)  throw err; 

  // install middleware
  swaggerExpress.register(app);

  var port = process.env.PORT || 10010;
  app.listen(port);


    console.log('Server started: http://127.0.0.1:' + port );

);

【问题讨论】:

你能解决这个问题吗? 【参考方案1】:
'use strict';

import errors from './components/errors';
import path from 'path';
var swaggerUi = require('swagger-ui-express');
var swaggerDocument = require('./swagger.js');

export default function(app) 
    // Insert routes below

    app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));



added this in route file...

【讨论】:

以上是关于Swagger - 更改项目目录的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 nestjs/swagger 更改查询参数序列化?

如何在 ASP.NET Core 中更改 Swagger 的基本 url

Golang Gin 项目使用 Swagger

Swagger2 更改 Swagger Ui 的基本路径

thinkphp 集成swagger-ui

基于HttpRunner,解析swagger数据,快速生成接口测试框架