Swagger - 更改项目目录

时间:2016-06-29 18:20:34

标签: node.js swagger swagger-ui swagger-2.0 swagger-editor

我在工作区内的根目录中创建了新项目( 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' }

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

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

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 个答案:

答案 0 :(得分:0)

'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...