2015-10-04 36 views
0

我有後續 - 由Visual Studio的節點工具最初產生,使用Express和玉石爲客戶錯誤:passport.initialize()中間件未使用什麼命令調用?

/** 
* Module dependencies. 
*/ 

var express   = require('express'); 
var fs    = require('fs'); 
var routes   = require('./routes'); 
var user   = require('./routes/user'); 
var http   = require('http'); 
var https   = require('https'); 
var path   = require('path'); 
var passport  = require('passport'); 
var googleStrategy = require('passport-google-oauth').OAuth2Strategy; 
var loginHandler = require('./routes/Login.js'); 
var auth   = require('./config/auth.js'); 
var googleSupport = require('./googleSupport.js'); 

var googleAuthority = auth.googleAuth; 
var googleScopes = ''; 


// retrieve google scopes 
googleScopes = googleSupport.discoverServiceScopes(auth.googleAuth); 

// set up passport 
passport.serializeUser(function (user,done) { 
    done(null, user); 
}); 

passport.deserializeUser(function (obj, done) { 
    done(null, obj); 
}); 

passport.use(new googleStrategy({ 
    clientID: googleAuthority.clientId, 
    clientSecret: googleAuthority.clientSecret, 
    callbackURL: googleAuthority.callbackUrl 
}, 
function (accessToken, refreshToken, profile, done) { 
    return done(null, profile); 
} 
)); 
var request = require('request'); 

var app = express(); 

// all environments 
app.set('port', process.env.PORT || 3000); 
app.set('views', path.join(__dirname, 'views')); 
app.set('view engine', 'jade'); 
app.use(express.favicon()); 
app.use(express.logger('dev')); 
app.use(express.json()); 
app.use(express.urlencoded()); 
app.use(express.methodOverride()); 
app.use(app.router); 
app.use(require('stylus').middleware(path.join(__dirname, 'public'))); 
app.use(express.static(path.join(__dirname, 'public'))); 

// development only 
if ('development' == app.get('env')) { 
    app.use(express.errorHandler()); 
} 

console.log('extracting service scopes'); 

app.get(passport.initialize()); 
app.get(passport.session()); 

app.get('/', routes.index); 
app.get('/index', routes.index); 

// google login support 

// go to login page. 
app.get('/googleLogin', passport.authenticate('google', { scope: [googleScopes] }), 
    function (req, res) { 
     res.redirect('/'); 
    } 
); 

app.get('/AuthorizeGoogle', passport.authenticate('google', { failureRedirect: '/'}), 
    function (req, res) { 
     res.redirect('/'); 
    }); 


http.createServer(app).listen(app.get('port'), function() { 
    console.log('Express server listening on port ' + app.get('port')); 
}); 

的事情是,當它驗證用戶得到以下

500 Error: passport.initialize() middleware not in use 
at IncomingMessage.req.login.req.logIn (C:\Node\YourLivesN\YourLivesN\node_modules\passport\lib\http\request.js:44:34) 
at Strategy.module.exports.strategy.success (C:\Node\YourLivesN\YourLivesN\node_modules\passport\lib\middleware\authenticate.js:228:13) 
at verified (C:\Node\YourLivesN\YourLivesN\node_modules\passport-oauth\node_modules\passport-oauth2\lib\strategy.js:179:18) 
at Strategy._verify (C:\Node\YourLivesN\YourLivesN\app.js:41:12) 
at C:\Node\YourLivesN\YourLivesN\node_modules\passport-oauth\node_modules\passport-oauth2\lib\strategy.js:195:22 
at C:\Node\YourLivesN\YourLivesN\node_modules\passport-google-oauth\lib\passport-google-oauth\oauth2.js:115:7 
at passBackControl (C:\Node\YourLivesN\YourLivesN\node_modules\passport-oauth\node_modules\passport-oauth2\node_modules\oauth\lib\oauth2.js:125:9) 
at IncomingMessage. (C:\Node\YourLivesN\YourLivesN\node_modules\passport-oauth\node_modules\passport-oauth2\node_modules\oauth\lib\oauth2.js:143:7) 
at IncomingMessage.emit (events.js:129:20) 
at _stream_readable.js:908:16 

現在我認爲這與passport.js passport.initialize() middleware not in use這個問題的原因是一樣的,但是看不到我的代碼中應該有哪些命令。

所以任何人都可以告訴我以什麼順序放置代碼的各個部分。

感謝

+0

在這裏找到答案,幾年前: http://stackoverflow.com/questions/16781294/passport-js-passport-initialize-middleware-not-in-use 你的問題可能是如何你訂購了你的應用表達式。 – deek

+0

這是我在原來的問題中鏈接到的問題,但我不確定應該把我的調用的順序 - 我見過的大多數示例都使用app.confgure,但由Visual Studio創建的代碼不會將所有人都放入這 - 所以我不知道我應該使用什麼命令。 –

+0

app.configure已棄用。重新閱讀鏈接或查看快遞主頁的文檔http://expressjs.com/guide/routing.html – deek

回答

相關問題