2017-06-04 64 views
-1

我目前正在嘗試建立一個簡單的用戶認證系統與node.js和護照,並表示爲預定的SMS應用程序的進程;然而,試圖初始化服務器時,不管修復的組合,我用Google搜索,並試圖,cmdPrompt不斷渲染試圖建立與快遞和護照節點認證

$節點APPAUTH C:\用戶\ zobri \ uabcprojects \ nodeauth \ node_modules \表現\ LIB \路由器\ 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')); 
 
});

+0

哪條線是458? –

+0

@CharlieFish - 我還沒有編寫這麼多行代碼,我也沒有在這個目錄中的本地文件稱爲索引...必須是NPM代碼 –

+0

你到目前爲止嘗試過什麼? –

回答

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

這兩行是不正確的。您不想使用中間件來設置您的路線。

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

要設置我的路線,我使用類似上面的代碼。當然取代你的路徑。如果你使用這種方法,你也不需要下面兩行。

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

當代碼調試好的技巧是開始刪除代碼或評論它,直到你縮小問題到特定的代碼部分工作。然後從那裏工作。