尝试使用express和passport设置节点身份验证

时间:2017-06-04 22:50:11

标签: node.js express passport.js

我目前正在尝试使用node.js和passport建立一个简单的用户身份验证系统,并为预定的SMS应用程序表达;但是,在尝试初始化服务器时,无论我使用Google搜索和尝试修复的组合,cmdPrompt都会继续渲染

  

$ node appauth C:\ Users \ zobri \ uabcprojects \ nodeauth \ node_modules \ express \ lib \ router \ index.js:458         抛出新的TypeError('Router.use()需要中间件函数,但得到'+ gettype(fn));         ^

我不知道为什么......我一直在关注一个教程,但似乎无法弄清楚到底发生了什么 - 任何帮助将不胜感激! 罗伯特

我的代码:

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'));
});

1 个答案:

答案 0 :(得分:1)

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

Those two lines are incorrect. You aren't wanting to use Middleware to setup your routes.

require('./app/routefile1.js')(app, passport);
require('./app/routefile2.js')(app, passport);

To setup my routes I use something like the code above. Of course replacing with your paths. If you use this method you shouldn't need the two lines below also.

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

When working with code a good debugging trick is to start removing code or commenting it out until you narrow the problem down to a specific part of code. Then work from there.