node package manager



Extremely opinionated resource middleware for express and mongoDB.

For the specific use case where you want to provide view, validator, and persistence middleware for a resource.

This was created to save time when implementing a long multi-step form process.

build status


easily provide resources for a form:

var app = express();
var viewPath = 'form';
var selectOptions = ['Yes', 'No', 'Maybe'];
var fc = new FormMiddleware()
  .field({type: 'select', name: 'testField', options: selectOptions })
  .validator({fn: 'notEmpty', param: 'testField', msg: 'Test field cannot be empty'})
  .validator({fn: 'isIn', param: 'testField', msg: 'Valid option required', }, selectOptions)
  .save('testField', function(req, res) { return req.testObj })
  .next(function(savedObj, req, res) {return res.redirect('/' +; })
var m = fc.middleware();
app.get('/', m.render);'/', m.validateAndSave);


var FormMiddleware = require('express-mongodb-form-middleware');

var fm = new FormMiddleware()

Create a new FormMiddleware object.

The returned object fm is a FormMiddleware.


Set the viewPath to location of the view on disk.


Add a field that can be passed to the view, validated, and/or saved.

opts.locals would be any properties the view might use.


The Field that get's created has a getViewOpts method that returns all the assigned locals along with the name property.


Validate a field. Uses node-validator validators.

fm.update(field, fn)

Updates the property on the object with the given field.

field can be an array of field names.

fn is a function that returns the object on which field is to be updated. fn provides req, res as parameters.[, hookFn])

Save (persists) an object to the database. This is where you would save the parent object [optionally, after updating an embedded object].

fn is a function that returns the object to be saved. fn provides req, res as parameters.

hookFn is an optional function to do anything that might need to be done before saving. ie. push()ing an embedded object into a parent object.

Specify a function callback to handle the final step after all the objects have been saved.

fn provides savedObj, req, res as parameters, where savedObj is the object that was just saved.

Note: will not be called if there was an error from the database. In the event of an error the middleware will call next(err) internally.

var middleware = fm.middleware()

Creates the middleware based on the configured FormMiddleware object.

middleware.render(req, res, next)

This middlware will render the provided view.

middleware.validate(req, res, next)

This middlware will validate the configured params.

middleware.update(req, res, next)

This middleware will update the configured params on the given objects., res, next)

This middleware will persist the given objects to the database.


Returns an array of the validate, update, and save middleware.


With npm do:

npm install express-mongodb-form-middleware