passport.js 表示静态路径不可用

express static path not available to passport.js

我是这里的新手,所以请放轻松。 我有以下 server.js:

var express = require('express');
var app = express();
var path = require('path');

var port = process.env.PORT || 3001;
var cookieParser = require("cookie-parser");
var session = require('express-session');
var morgan = require("morgan");
var mongoose = require("mongoose");
var bodyParser = require("body-parser");
var passport = require("passport");
var flash = require("connect-flash");

app.use('/bower_components',  express.static(__dirname + '/bower_components'));
app.use('./config/auth',  express.static(__dirname + './config/auth'));
app.use('/app',  express.static(__dirname + '/app'));
app.set('views', __dirname + '/dist/views');
app.set('view engine', 'jade');

require('./config/passport')(passport);
app.use(morgan('dev'));
app.use(cookieParser());
app.use(bodyParser.urlencoded({extended:false}));
app.use(session({secret: 'anystringoftext',
                saveUninitialized: true,
                resave: true}));

app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
app.use(express.static(__dirname + '/dist')); 

//app.use(express.static(path.join(__dirname, '/*')));
require('./app/routes.js')(app, passport);

app.listen(port);
console.log('server running on port:'+port);

我正在尝试让护照与 google 身份验证一起使用:

var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
var User = require('../app/models/user');
var configAuth = require('./config/auth');
module.exports = function(passport){
    passport.serializeUser(function(user, done){
        done(null, user.id);
    });
    passport.deserializeUser(function(id, done){
        User.findById(id, function(err, user){
            done(err, user);
        })
    });
    passport.use(new GoogleStrategy({
        clientID: configAuth.googleAuth.clientID,
        clientSecret: configAuth.googleAuth.clientSecret,
        callbackURL: configAuth.googleAuth.callbackURL
    }, function(accessToken, refreshToken, profile, done) {
        process.nextTick(function(){
            User.findOne({'google.id': profile.id}, function(err, user){
                if(err)
                    return done(err);
                if(user)
                    return done(null, user);
                else {
                    var newUser = new User();
                    newUser.google.id = profile.id;
                    newUser.google.token = accessToken;
                    newUser.google.name = profile.displayName;
                    newUser.google.email = profile.emails[0].value;

                    newUser.save(function(err){
                        if(err)
                            throw err;
                        return done(null, newUser);
                    })
                    console.log(profile);
                }
            });
        });
    }

   ));
}

我收到以下错误:

Error: Cannot find module './config/auth' at Function.Module._resolveFilename (module.js:325:15) at Function.Module._load (module.js:276:25) at Module.require (module.js:353:17) at require (internal/module.js:12:17) at Object. (/Users/toddcoulson/Documents/PTO/config/passport.js:3:18) at Module._compile (module.js:409:26) at Object.Module._extensions..js (module.js:416:10) at Module.load (module.js:343:32) at Function.Module._load (module.js:300:12) at Module.require (module.js:353:17) at require (internal/module.js:12:17) at Object. (/Users/toddcoulson/Documents/PTO/server.js:20:1) at Module._compile (module.js:409:26) at Object.Module._extensions..js (module.js:416:10) at Module.load (module.js:343:32) at Function.Module._load (module.js:300:12)

我试过调用 ./config/auth、/config/auth、/config,许多不同的组合,但它似乎没有读入配置授权文件。关于我的任何想法我失踪了? 我一直在关注这里的代码: https://github.com/BrentAureli/Youtube-Tutorials/tree/master/NodeJS%20Tutorials/Tutorial%2011%20-%20Google%20OAuth 但我不认为这正是我的情况,因为我在 express 中添加了浏览器同步,并使其正常工作。所以我不确定浏览器同步是否与此有关,但我想我会添加它。在此先感谢您的帮助。 在文件夹结构方面,我所有的静态文件都在 /dist 中,并且与 server.js 处于同一级别,我有一个文件夹应用程序和一个文件夹配置。

您从中调用 require('./config/auth');passport.js 文件似乎已经在 config 文件夹中。所以在 passport.js 的第三行,它应该只是 var configAuth = require('./auth');,因为文件路径是相对的。