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

express-ctrlr

v0.0.3

Published

Express controllers

Downloads

1

Readme

express-ctrlr

Build Status Code Climate David DM

Controllers for express.js (4.x.x)

Install

npm install express-ctrlr

Usage

Standard CRUD

var express = require('express');
var controller = require('express-ctrlr');
var app = express();

var postsCtrl = controller()
  .index(function(req, res, next) { ... })
  .new(function(req, res, next) { ... })
  .create(function(req, res, next) { ... })
  .show(function(req, res, next) { ... })
  .edit(function(req, res, next) { ... })
  .update(function(req, res, next) { ... })
  .patch(function(req, res, next) { ... })
  .destroy(function(req, res, next) { ... });

app.use('/posts', postsCtrl.router());

Maps routes

GET    /posts
GET    /posts/new
POST   /posts
GET    /posts/:id
GET    /posts/:id/edit
PUT    /posts/:id
PATCH  /posts/:id
DELETE /posts/:id

Define your own actions

var postsCtrl = controller()
  .action("/new", function(req, res, next) { ... });

app.use('/posts', postsCtrl.router());

// GET /posts/new

action defaults to the GET method. You can define the method VERB of choice.

var postsCtrl = controller()
  .action("/", {method: 'POST'}, function(req, res, next) { ... });

app.use('/posts', postsCtrl.router());

// POST /posts

Before actions

var postsCtrl = controller()
  .before(isAuthenticated())
  .before(csrf())
  .new(function(req, res, next) { ... })
  .create(function(req, res, next) { ... });

app.use('/posts', postsCtrl.router());

// /posts/* isAuthenticated()
// /posts/* csrf()
// GET  /posts/new
// POST /posts

Order matters, kind of...

var postsCtrl = controller()
  .create(function(req, res, next) { ... })
  .new(function(req, res, next) { ... })
  .before(isAuthenticated())
  .action("/", function(req, res, next) { ... })
  .before(csrf());

app.use('/posts', postsCtrl.router());

Will map in this order

// /posts/* isAuthenticated()
// /posts/* csrf()
// GET  /posts
// GET  /posts/new
// POST /posts

The mapping order is always

1 before(s)               (then in the order they were defined)
2 action(s)               (then in the order they were defined)
3 <built in crud methods> (this order: index, new, create, show, edit, update, patch, destroy)

Built in crud method order

1 index
2 new
3 create
4 show
5 edit
6 update
7 patch
8 destroy

The built in crud methods will always maintain their defined map order regardless of their definition order.

.show()
.new()

Will still map as

// GET /posts/new
// GET /posts/:id

Alternate path definition.

var ctrlr = controller()
  .new(...)
  .show(...)
  .create(...);

app.use(ctrlr.router("/posts"));

// GET  /posts/new
// GET  /posts/:id
// POST /posts

This method allows for a simple namespacing pattern.

app.use("/api", postsCtrlr.router("/posts"));
app.use("/api", tasksCtrlr.router("/tasks"));

// /api/posts/...
// /api/tasks/...

Nested routes.

var ctrlr = controller()
  .show(...);

app.use(ctrlr.router("/posts/:post_id/comments"));

// GET /posts/:post_id/comments/:id

Requires the path to be passed as the router argument in order to get all the URL params.

License

MIT