Forum Discussion

St_Nick's avatar
St_Nick
New Member
2 years ago

Can't get express-openapi to load my routes

Hi all.
I'm pretty new with openapi, and I'm trying to convert an existing express app to work with openapi.
I've followed the documentation, and tried converting 1 of my routes to openapi (while canceling all others, to make sure I'm not mixing both methods).
In a preview, I see the documentation working fine, but when I start the server, I can't reach my routes. I get 404 when I call http://localhost:4001/getDevices.

If I add a regular path /test and call that, not via openapi, it works as expected.

 

The structure of my project:
src/
src/api/getDevices/get.ts
src/openapi/openapi.yaml
src/openapi/components/schemas/Device.yaml
src/openapi/components/parameters/DeviceSearchQuery.yaml
src/openapi/paths/getDevices.yaml

I initialize the openapi in my Server class, as follows:

class Server {
  public app: Application;
  public server: http.Server;

  constructor() {
    this.app = express();
    this.server = http.createServer(this.app);
  }

  async start(port: number): Promise<void> {

    // Define the options for the initialize function
    const options = {
      apiDoc: join(__dirname, '../openapi/openapi.yaml'),
      app: this.app,
      paths: path.join(__dirname, '../api/'),
    };

    // Call the initialize function with the options
    initialize(options);

    // Start the server
    this.server.listen(port);

  }
}

export default new Server();

And start is called in my entry point.

openapi.yaml:

openapi: 3.0.0
info:
  title: AWS Proxy API
  version: 1.0.0
servers:
  - url: http://localhost:4001
    description: Local server

tags:
  - name: Devices
    description: Device management

paths:
  /getDevices:
    $ref: './paths/getDevices.yaml'

components:
  parameters:
    DeviceSearchQuery:
      $ref: './components/parameters/DeviceSearchQuery.yaml'
  schemas:
    Device:
      $ref: './components/schemas/Device.yaml'

 
getDevices.ts:

export async function getDevices(request: Request, response: Response): Promise<void> {
  // some logic
}

 

getDevices.yaml:

get:
  tags:
    - Devices
  operationId: getDevices
  parameters:
    - $ref: '../components/parameters/DeviceSearchQuery.yaml'
  responses:
    '200':
      description: Successful operation
      content:
        application/json:
          schema:
            type: array
            items:
              $ref: '../components/schemas/Device.yaml'
    '400':
      description: Invalid parameters
    '500':
      description: Server error


I kinda got lost at this point, I might have mixed some things up already, but I'm really not sure what I'm not doing wrong here.
I'd love some assistance to understand my error, and what I should be doing instead of what I already am.

 

Thank you! (and be gentle, I'm new)

No RepliesBe the first to reply