Closed fela98 closed 8 years ago
These handlers bind to the api specification.
Generally, the info is returned from the docspath
property, which will return the swagger spec.
Is that what you are looking for?
No, not exactly @tlivings I am not looking to get the swagger spec, or the documentation of the API. Forget what I said about getting information about the API...
What I am looking for is to simply be able to add a handler for the '/' (root) request. Until now I've just been using a simple express router to add a handler. However, I want to add the handler by using swaggerize express and add it as a property to the handlers
object.
I want to do:
app.use(swaggerize({
api: spec,
handlers: {
'/': {
'$get': function(req, res) {
res.send(something);
}
}
}));
But I currently have to do the following before initializing swaggerize express:
express.get('/', function(res, req, next) {
res.send(something);
})
I hope that this clarifies what my problem is.
Oops... I accidentally closed the issue...
The swaggerize module is used to bind routes defined in the API spec to handlers.
You can't define a handler that doesn't have a corresponding API entry. Instead you must do it as you mentioned above.
It does in fact have a route defined in the API spec. It also shows up in the Swagger UI.
It should also be allowed according to the Swagger Spec 2.0 where it says that the pattern is "/{path}"
, where "{path}"
in this case is an empty string, resulting in "/"
"paths": {
"/": {
"get": {
"tags": [
"about"
],
"summary": "Get a status of the API",
"description": "Gets status and other useful information about the API",
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "Status acquired",
"schema": {
"type": "object"
}
}
}
}
}
}
And what is the base path in your swagger doc?
My "basePath"
is not present. This means, according to the swagger spec, that the "host"
value will be used. This is not specified either which means that the API will be served on the same host that serves the documentation, which also is the case.
I have also tried the following:
app.use(swaggerize({
api: spec,
handlers: {
'$get': function(req, res) {
res.send(something);
}
}
));
And another question, how should this be done if you use js files as handlers instead of an object?
The only logical way I could think of sould be to use the index.js
file for an eventual root request
I've tried doing it like this, but with no success:
app.use(swaggerize({
api: spec,
handlers: './handlers'
));
Where ./handlers
contains the following file named index.js
module.exports = {
'get': function(req, res) {
res.send(something);
}
};
Everything I tried simply resulted in a Cannot GET /
response from express.
Have you tried following the swagger spec such that you've defined the path as /{path}
and allow the param to be empty?
I've added a PR to swaggerize-routes
that fixes this.
Landed in swaggerize-routes@1.0.6
.
I know this is an old dead thread, but I'm having the same problem, it might help some one out there.
After some digging with the source code, turns out you can just add a x-handler property to you YAML or JSON file like this x-handler: [handlers_directory]/[fileName.js]
Here is an example from my YAML file
/:
x-handler: controllers/index.js # defines a specific handler for swaggerize routes
get:
x-handler: controllers/index.js
description: Server thread uptime information and healthcheck URL.
operationId: get
....reset of your YAML file
You can check that in the swaggerize routes test folder
I wish this can be added the docs so it will make it easier for users.
Cheers.
I usually create a root request, that is some information about the API that is returned when a GET is made to '/' of the API. I can specify this in my swagger.json file. However adding a listener to it did not seem to work :/
I tried:
but with no luck. I also tried an empty string...
Is there any other way, than adding a handler manually?