Extremely opinionated resource middleware for express and mongoDB.


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.


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(reqres) { return req.testObj })
  .next(function(savedObjreqres) {return res.redirect('/' +; })
var m = fc.middleware();
app.get('/', m.render);'/', m.validateAndSave);


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

Create a new FormController.

The returned object fm is a FormController.

Set the viewPath to location of the view on disk.

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

Validate a field. Uses node-validator validators.

Save fields to the database.

field is the field to save from the form into the database. Optionally, field can be an array of field names.

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

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.


With npm do:

npm install express-mongodb-form-middleware