npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

convexpress

v2.3.0

Published

Employ conventions to register express routes

Downloads

486

Readme

npm build status coverage dependencies devDependencies

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=src
const dbClient = require("services/db");

exports.path = "/pets";
exports.method = "get";
exports.description = "List pets";
exports.tags = ["pets"];
exports.responses = {
  "200": {
    description: "pets list"
  }
};
exports.parameters = [
  {
    name: "status",
    description: "Filter by pet status (e.g. available / not available)",
    in: "query",
    required: false,
    type: "string"
  }
];
exports.handler = async (req, res) => {
  const pets = await dbClient.query(`SELECT * FROM pets WHERE status = $1`, [
    req.query.status
  ]);
  res.status(200).send(pets);
};

Register the route and serve it

/* File: src/server.js */
const express = require("express");
const convexpress = require("convexpress");

const options = {
  info: {
    title: "pet store",
    version: "1.0.0"
  },
  host: "localhost:3000"
};
const api = convexpress(options)
  // Serve the API's swagger definition at /swagger.json
  .serveSwagger()
  // Register the route (assuming NODE_PATH=src)
  .convroute(require("api/pets/get"))
  // Or register multiple routes at once
  .loadFrom(`${__dirname}/api/**/*.js`);

const server = express()
  .use(api)
  .listen(process.env.PORT);

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
    • host string
    • basePath string
    • info string
    • bodyParserOptions object: options for the json body parser:
      • limit string (default 100kb): maximum body size (details)
      • strict boolean (default true): strictly parse the json body (details)
      • verify function: a function that verifies the body (details)
Returns

The express router (convrouter).

convrouter.convroute(convroute)

Registers a convroute.

Arguments
  • convroute object required: a convroute definition object:
    • path string required
    • method string required
    • handler function _required__
    • paramters Array< object >
    • middleware Array< function >
    • description string
    • tags 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.