gpt4 book ai didi

javascript - express 发送 Passport 到 app.use(route)

转载 作者:行者123 更新时间:2023-11-30 15:53:06 25 4
gpt4 key购买 nike

我正在尝试将 Passport 发送到 app.use('/api')(passport) 但这样做时我似乎遇到了错误。我应该将它发送到 app.use() 还是我可以在 profile.js 文件中 recuire() 它?我是表达新手,正在学习如何实现 passport.js 的教程。

F:\App\node_modules\express\lib\router\index.js:140
var search = 1 + req.url.indexOf('?');
^
TypeError: Cannot read property 'indexOf' of undefined
at Function.handle (F:\App\node_modules\express\lib\router\index.js:140:27)
at EventEmitter.handle (F:\App\node_modules\express\lib\application.js:173:10)
at app (F:\App\node_modules\express\lib\express.js:38:9)
at ExpressRouter (F:\App\app\backend\router\routes.js:15:31)
at Object.<anonymous> (F:\App\app\backend\backend.app.js:36:27)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Module.require (module.js:468:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (F:\App\app\server.js:8:11)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)

服务器.js

var config = require('./config/config');
var logger = require('morgan');
var path = require('path');
var express = require('express');
var app = require('./backend/backend.app.js');
app.use(logger('dev'));
app.use(express.static('../'));
app.use(express.static(path.join(__dirname, './frontend')));
app.all('/*', function (req, res) {
res.sendFile(path.join(__dirname, './frontend/index.html'));
});

后端.app.js

var express = require('express');
var app = express();
var passport = require('passport');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
var config = require('../config/config');
var flash = require('connect-flash');
require('./models/user');
require('./lib/passport')(passport);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));
app.use(cookieParser());
app.use(session({ secret: 'secret',resave: true, saveUninitialized: true }));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
require('./router/routes')(app, passport);
module.exports = app;

路由.js

var profile = require('./routes/profile');
var isLoggedIn = function(req, res, next){
if(req.isAuthenticated())
return next();
res.redirect('/');
};
var ExpressRouter = function(app, passport){
app.use('/api', profile)(passport);
};
module.exports = ExpressRouter;

配置文件.js

var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var User = mongoose.model('User');

module.exports = function (passport){
router.post('/signup', passport.authenticate('local-signup', {
successRedirect : '/',
failureRedirect : '/signup',
failureFlash : true
}));
return router;
};

最佳答案

在您的情况下,您必须使用路由器对象作为第二个参数来调用 app.use。所以,而不是

app.use('/api', profile)(passport);

你应该在你的 routes.js 文件中这样写:

app.use('/api', profile(passport));

由于 profile(passport) 返回一个快速路由器对象。

关于javascript - express 发送 Passport 到 app.use(route),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39007043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com