Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

express-classy

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-classy

A set of classy route handlers

  • 2.2.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
10
increased by400%
Maintainers
1
Weekly downloads
 
Created
Source

After building quite a few APIs with Node.js and Express over the last couple years, I started forming some strong opinions about what I liked and disliked. I had gotten very familiar with the async library, and rightly so. It's a great tool for maintaining control in the face of all those callbacks. However, I was always left wanting something that felt a little more structured. I wanted a pattern, something I could lean on to standardize the way I do things. Since API routes have common high-level components (validating a request, performing an action in the database, returning a response, etc), express-classy is my take on organizing and controlling the logic flow in Express route handlers.

Example

add_person.js:

var util = require( "util" );
var CreateHandler = require( "express-classy" ).CreateHandler;


module.exports = AddPerson;


function AddPerson () {
    CreateHandler.call( this );
}

util.inherits( AddPerson, CreateHandler );

AddPerson.prototype.validate = function() {

    this.req.checkBody( "firstName", "'firstName' is required" ).isLength( 1 );
    this.req.checkBody( "lastName", "'lastName' is required" ).isLength( 1 );

    var validationErrors = this.req.validationErrors();

    if ( validationErrors ) {
        return this.emit( "invalid", validationErrors );
    }

    var person = {
        firstName: this.req.body.firstName,
        lastName: this.req.body.lastName
    };

    return this.emit( "create", person );

};

AddPerson.prototype.action = function( person ) {

    // add 'person' to the database here
    // if error, emit "error", passing the error
    // if successful, emit "done", passing the data for response

};

routes.js:

var express = require( "express" );
var router = express.Router();
var AddPerson = require( "./add_person" );


var addPerson = new AddPerson();
router.post( "/person", function ( req, res, next ) {
    addPerson.handle( req, res, next );
} );


module.exports = router;

Keywords

FAQs

Package last updated on 11 Apr 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc