Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not able to get Server.swagger(...) to expose /api-docs endpoint #18

Closed
cyphercider opened this issue Jun 7, 2017 · 8 comments
Closed
Labels

Comments

@cyphercider
Copy link

I was able to get swaggerGen to work, as well as normal routes using the @path decorator. However, I'm not able to expose the swagger endpoint using the example in the readme. Wondering if you could tell me what I'm doing wrong? Here is my server setup:

// server.ts
let expressApp: express.Express = express()
middleware.mountMiddleware(expressApp)
Routes.mountRoutes(expressApp, port)

let server = http.createServer(expressApp)
let io = require('socket.io')(server)
sockets.registerSockets(io)

server.listen(port, '0.0.0.0', function () {
	console.log('Express started on port ' + port)
})
// middleware.ts
export function mountMiddleware (expressApp) {
	// expressApp.use(morgan('combined'))
	wlog.log('debug', 'serving from static path ' + serverConfig.publicPath)
	expressApp.use('/', express.static(serverConfig.publicPath))
	expressApp.use(BodyParser.json())
	expressApp.use(cors())
}
// routes.ts
export function mountRoutes (app, port) {
	Server.buildServices(app)
	wlog.log('debug', 'setting up Server.swagger from cwd: ' + process.cwd())
	Server.swagger(app, './swagger/api/swagger/swagger.yaml', '/api-docs', '0.0.0.0:5674', ['http'])
}

GET to http://localhost:5674/api-docs results in ERR_CONNECTION_REFUSED
GET to http://localhost:3000/api-docs (just in case the route was exposed on the same port as the other routes) results in a status 404

@pdusen
Copy link

pdusen commented Jun 7, 2017

We're running into a similar issue.

// app.ts
import * as express from 'express';
import { Server } from 'typescript-rest';

import './api/controllers';

const app: express.Application = express();
Server.buildServices(app);
Server.swagger(app, './dist/swagger.yaml', 'api-docs');

app.listen('4000', () => {
  console.log('REST Server listening on port 4000!');
});

Looking at the debug output, it appears to me as though the route is not registering in the same way as the API routes. Compare /api-docs with /hello in the log:


  express:application set "x-powered-by" to true +0ms
  express:application set "etag" to 'weak' +7ms
  express:application set "etag fn" to [Function: wetag] +11ms
  express:application set "env" to 'development' +7ms
  express:application set "query parser" to 'extended' +7ms
  express:application set "query parser fn" to [Function: parseExtendedQueryString] +8ms
  express:application set "subdomain offset" to 2 +11ms
  express:application set "trust proxy" to false +5ms
  express:application set "trust proxy fn" to [Function: trustNone] +11ms
  express:application booting in development mode +7ms
  express:application set "view" to [Function: View] +9ms
  express:application set "views" to 'C:\\Repositories\\application\\views' +5ms
  express:application set "jsonp callback name" to 'callback' +10ms
  express:router use '/' query +15ms
  express:router:layer new '/' +7ms
  express:router use '/' expressInit +7ms
  express:router:layer new '/' +5ms
  express:router:route new '/hello/:name' +6ms
  express:router:layer new '/hello/:name' +5ms
  express:router:route get '/hello/:name' +7ms
  express:router:layer new '/' +6ms
  express:router:layer new '/' +9ms
  express:router:route new '/hello/:name' +7ms
  express:router:layer new '/hello/:name' +4ms
  express:router:route acl '/hello/:name' +8ms
  express:router:layer new '/' +5ms
  express:router:route bind '/hello/:name' +8ms
  express:router:layer new '/' +5ms
  express:router:route checkout '/hello/:name' +5ms
  express:router:layer new '/' +9ms
  express:router:route connect '/hello/:name' +5ms
  express:router:layer new '/' +5ms
  express:router:route copy '/hello/:name' +5ms
  express:router:layer new '/' +4ms
  express:router:route delete '/hello/:name' +7ms
  express:router:layer new '/' +5ms
  express:router:route get '/hello/:name' +6ms
  express:router:layer new '/' +6ms
  express:router:route head '/hello/:name' +5ms
  express:router:layer new '/' +5ms
  express:router:route link '/hello/:name' +5ms
  express:router:layer new '/' +5ms
  express:router:route lock '/hello/:name' +6ms
  express:router:layer new '/' +5ms
  express:router:route m-search '/hello/:name' +7ms
  express:router:layer new '/' +4ms
  express:router:route merge '/hello/:name' +7ms
  express:router:layer new '/' +4ms
  express:router:route mkactivity '/hello/:name' +4ms
  express:router:layer new '/' +7ms
  express:router:route mkcalendar '/hello/:name' +7ms
  express:router:layer new '/' +5ms
  express:router:route mkcol '/hello/:name' +5ms
  express:router:layer new '/' +4ms
  express:router:route move '/hello/:name' +5ms
  express:router:layer new '/' +62ms
  express:router:route notify '/hello/:name' +3ms
  express:router:layer new '/' +2ms
  express:router:route options '/hello/:name' +4ms
  express:router:layer new '/' +3ms
  express:router:route patch '/hello/:name' +2ms
  express:router:layer new '/' +2ms
  express:router:route post '/hello/:name' +5ms
  express:router:layer new '/' +1ms
  express:router:route propfind '/hello/:name' +2ms
  express:router:layer new '/' +5ms
  express:router:route proppatch '/hello/:name' +1ms
  express:router:layer new '/' +5ms
  express:router:route purge '/hello/:name' +2ms
  express:router:layer new '/' +4ms
  express:router:route put '/hello/:name' +1ms
  express:router:layer new '/' +5ms
  express:router:route rebind '/hello/:name' +1ms
  express:router:layer new '/' +5ms
  express:router:route report '/hello/:name' +1ms
  express:router:layer new '/' +4ms
  express:router:route search '/hello/:name' +2ms
  express:router:layer new '/' +4ms
  express:router:route subscribe '/hello/:name' +1ms
  express:router:layer new '/' +5ms
  express:router:route trace '/hello/:name' +1ms
  express:router:layer new '/' +5ms
  express:router:route unbind '/hello/:name' +4ms
  express:router:layer new '/' +2ms
  express:router:route unlink '/hello/:name' +4ms
  express:router:layer new '/' +1ms
  express:router:route unlock '/hello/:name' +5ms
  express:router:layer new '/' +2ms
  express:router:route unsubscribe '/hello/:name' +4ms
  express:router:layer new '/' +2ms
  express:router:route new '\\api-docs\\json' +22ms
  express:router:layer new '\\api-docs\\json' +1ms
  express:router:route get '\\api-docs\\json' +2ms
  express:router:layer new '/' +12ms
  express:router:route new '\\api-docs\\yaml' +1ms
  express:router:layer new '\\api-docs\\yaml' +6ms
  express:router:route get '\\api-docs\\yaml' +8ms
  express:router:layer new '/' +2ms
  express:router use '\\api-docs' serveStatic +10ms
  express:router:layer new '\\api-docs' +2ms
  express:router use '\\api-docs' <anonymous> +2ms
  express:router:layer new '\\api-docs' +3ms
REST Server listening on port 4000!

@cyphercider
Copy link
Author

@pdusen , are you running windows? We ran into an issue with Path.join here: thiagobustamante/typescript-rest-swagger#3, where the path library was inserting '\' instead of '/' when running on Windows.

@pdusen
Copy link

pdusen commented Jun 7, 2017

@tamethecomplex I am running Windows, and I independently reached a similar conclusion.

@pdusen
Copy link

pdusen commented Jun 7, 2017

Locally, I was able to resolve my problem by changing the implementation of Server.swagger to use path.posix.join instead of the current path.join:

  router.get(path.posix.join('/', endpoint, 'json'), (req, res, next) => {
    res.send(swaggerDocument);
  });
  router.get(path.posix.join('/', endpoint, 'yaml'), (req, res, next) => {
    res.set('Content-Type', 'text/vnd.yaml');
    res.send(YAML.stringify(swaggerDocument, 1000));
  });

  router.use(path.posix.join('/', endpoint), swaggerUi.serve, swaggerUi.setup(swaggerDocument));

@cyphercider
Copy link
Author

Just confirmed the same myself.

@pdusen
Copy link

pdusen commented Jun 7, 2017

Just opened a PR with this change: #19

@thiagobustamante
Copy link
Owner

Thanks. I already merged the code.

@thiagobustamante
Copy link
Owner

thiagobustamante commented Jun 7, 2017

Fixed in version 1.0.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants