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 🙏

© 2026 – Pkg Stats / Ryan Hefner

express-class-views

v0.3.0

Published

Class-based views for ExpressJS.

Downloads

10

Readme

express-class-views

A simple library to add class-based view support for ExpressJS.

If you're familiar with ExpressJS views/routing you're probably accustomed to a pattern like:

let router = express.Router();

router.route('/')
  .get(function(req, res, next) {
    // Same handler code.
  })
  .post(function(req, res, next) {
    // Same handler code.
  });

There are a couple subjective issues here:

  1. Individual verbs are somewhat awkward to document.
  2. ExpressJS feels that verbs that aren't explicitly handled should return 404 errors (as opposed to 405 errors).
  3. Variables pertinent to multiple verb handlers are more difficult to organize.
  4. OPTIONS handlers need to be manually specified.

All this library does is rectify these behaviors while piggybacking on the otherwise straightforward ExpressJS pattern.

So re-tooling the example above:

const View = require('express-class-views');

class MyView extends View {
  get(req, res, next) {
    // Same handler code.
  }

  post(req, res, next) {
    // Sample handler code.
  }
}

let router = express.Router();

// Generate middleware function for class and map it to "all" HTTP verbs.

router.route('/')
  .all(MyView.handler());

// OR

router.route('/')
  .use(MyView.handler());

Any verbs (besides OPTIONS which is automatic) which are not specifically defined as a class method will generate a 405 error by way of the http-errors module.

If you'd like to specify the error library that will be used to create the 405 error (which may affect instanceof checks elsewhere in your application code) you can pass in the option errors to the .handler() static method.

MyView.handler({
  errors: require('http-errors')
});

You can also specify a property on the constructor.

MyView.errors = require('http-errors');

The only requirements for these options:

  1. Needs to be a function.
  2. Should accept an integer as the first argument to specify which error gets created.

Overriding OPTIONS

The OPTIONS verb will be handled by default and will send a simple Accepts header including all the verbs specified on your view class. You can easily override this behavior by specifying a class method for the OPTIONS verb.

const View = require('express-class-views');

class MyView extends View {
  options(req, res, next) {
    // Overriding behavior.
  }

  get(req, res, next) {
    // Same handler code.
  }

  post(req, res, next) {
    // Sample handler code.
  }
}

Scoping Variables

You can easily tie variables to a particular class inside of a constructor.

const View = require('express-class-views');

class MyView extends View {
  constructor() {
    this.someVariable = 'foo';
  }

  get(req, res, next) {
    // Same handler code.

    this.someVariable === 'foo'; // true
  }
}

Whenever you call the class method .handler() an instance of your view class is created. For every method handler the context this will be the instance of your view. So any variables you add in the constructor will be available there.

What's Left

There are still some pain points that may be addressed in future versions.

For instance it's still cumbersome to prepend your class views with middleware to handle behaviors. Let's say you wanted to check that a request is logged in with some middleware function loginRequred on all verbs and validate CSRF tokens with another middleware function csrfValidate on POST and PUT.

You'd need to do something irritating like:

let router = express.Router();

// Generate middleware function for class and map it to "all" HTTP verbs.
router.route('/')
  .all(loginRequired)
  .post(csrfValidate)
  .put(csrfValidate)
  .all(MyView.handler());

There are proposals to future ES specifications that might make this more straightforward with annotations.

@loginRequired
class MyView extends View {
  get(req, res, next) {
    // Some handler code.
  }

  @csrfValidate
  post(req, res, next) {
    // Same handler code.
  }

  @csrfValidate
  put(req, res, next) {
    // Same handler code.
  }
}

But for now we wait.