nodejs

Nodejsよく使うライブラリーまとめ(メモ)

var expressValidator = require('express-validator');
var session = require('express-session');
var messages = require('express-messages');
var flash = require('connect-flash');
var passport = require('passport');
var localStrategy = require('passport-local').Strategy;
var async = require('async');
var mongo = require('mongodb');
var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/elearn');
var db = mongoose.connection;


***// Express Session***
app.use(session({
    secret: 'secret',
    saveUninitialized: true,
    resave: true
}));


***// Express Validator***
app.use(expressValidator({
  errorFormatter: function(param, msg, value) {
      var namespace = param.split('.')
      , root    = namespace.shift()
      , formParam = root;

    while(namespace.length) {
      formParam += '[' + namespace.shift() + ']';
    }
    return {
      param : formParam,
      msg   : msg,
      value : value
    };
  }
}));


***// Connect-Flash***
app.use(flash());
app.use(function (req, res, next) {
  res.locals.messages = require('express-messages')(req, res);
  next();
});