convexpress
Employ conventions to register express routes. This is done by creating route definition objects - convroutes - which:
- register the route's method and path
- handle input validation
- document the route
Install
npm install --save convexpress
Note: this library requires nodejs >= 8
Use
Define a route (convroute)
/* File: src/api/pets/get.js */// Assuming NODE_PATH=srcconst dbClient = ; exportspath = "/pets";exportsmethod = "get";exportsdescription = "List pets";exportstags = "pets";exportsresponses = "200": description: "pets list" ;exportsparameters = name: "status" description: "Filter by pet status (e.g. available / not available)" in: "query" required: false type: "string" ;exportshandler = async { const pets = await dbClient; resstatus200;};
Register the route and serve it
/* File: src/server.js */const express = ;const convexpress = ; const options = info: title: "pet store" version: "1.0.0" host: "localhost:3000";const api = // Serve the API's swagger definition at /swagger.json // Register the route (assuming NODE_PATH=src) // Or register multiple routes at once ; const server = ;
API
convexpress(options)
Create an express router object (convrouter), which the additional methods
convroute
, serveSwagger
, and loadFrom
.
Arguments
options
object: top-level properties of the swagger definition
Returns
The express router (convrouter).
convrouter.convroute(convroute)
Registers a convroute.
Arguments
convroute
object required: a convroute definition object:path
string requiredmethod
string requiredhandler
function _required__paramters
Array< object >middleware
Array< function >description
stringtags
Array< string >responses
Map< object >
Returns
The convrouter, to allow for method chaining.
convrouter.serveSwagger()
Registers the route GET /swagger.json
for serving the swagger definition, and
the route GET /swagger/
for serving the swagger UI html.
Arguments
None.
Returns
The convrouter, to allow for method chaining.
convrouter.loadFrom(pattern)
Loads and registers convroutes from files matching the specified pattern
.
Arguments
pattern
string required: glob pattern of files to load convroutes from
Returns
The convrouter, to allow for method chaining.
Contributing
See CONTRIBUTING.md.