Sunday, 4 June 2017

Trying to set up Node authentication with express and passport

Im currently in the proccess of trying to build a simple user authentication system with node.js and passport and express for a scheduled SMS application; however, when trying to initialize server, no matter the combination of fixes I have googled and tried, cmdPrompt keeps rendering

$ node appauth C:\Users\zobri\uabcprojects\nodeauth\node_modules\express\lib\router\index.js:458 throw new TypeError('Router.use() requires middleware function but got a ' + gettype(fn)); ^

I have no clue as to why... I have been following a tutorial, but can't seem to figure out what the heck is happening - any help would be greatly appreciated! Robert

My code:

const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const expHandlebars = require('express-handlebars');
const expValidator = require('express-validator');
const expSession = require('express-session');
const flash = require('connect-flash');
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const database = require('mongodb');
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/appAuth');
const db = mongoose.connection;



// routes 
const routes = require('./routes/index');
const users = require('./routes/users');

// initialize application
const app = express();

// view engine 
app.set('views', path.join(__dirname, 'views'));
app.engine('handlebars', expHandlebars({ defaultLayout: 'layout' }));
app.set('view engine', 'handlebars');

// body-parser middleware 
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());

// set static directory 
app.use(express.static(path.join(__dirname, 'public')));

// express session
app.use(expSession({
     secret: 'rddremind',
     proxy: true,
     resave: true,
     saveUninitialized: true

}));

// passport init
app.use(passport.initialize());
app.use(passport.session());

// express validator (code snippet courtesy of 'https://github.com/ctavan/express-validator')\\

app.use(expValidator({
     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());

// Global variables 
app.use(function(req, res, next) {
          res.locals.success_msg = req.flash('success_msg');
          res.locals.error_msg = req.flash('error_msg');
          res.locals.error = req.flash('error');
          next();

     }),

     app.use('/', routes);
app.use('/users', users);

// set Port
app.set('port', (proccess.env.PORT || 8081));

app.listen(app.get('port'), function() {
     console.log('Server initialized on port ' + app.get('port'));
});


via Robert Zobrist

No comments:

Post a Comment