router.get 函数没有被调用
Posted
技术标签:
【中文标题】router.get 函数没有被调用【英文标题】:router.get function not being called 【发布时间】:2017-04-18 15:34:10 【问题描述】:我正在使用 passport.js 作为基本的登录/注销系统。出于某种原因,当我在 Advanced rest 客户端上运行 http://localhost:3000/auth/signout 时,没有调用 router.get('/signout', function(req, res))
。相反,我得到一个内部服务器错误:500。登录工作正常,但注销没有。这是我的 app.js 和 authenticate.js:
App.js
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var passport = require('passport');
var session = require('express-session');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
//Initialize models
require('./models/models.js');
var api = require('./routes/api');
var authenticate = require('./routes/authenticate')(passport);
var mongoose = require('mongoose');
//connect to mongodb
mongoose.connect("mongodb://localhost:27017/chirp-test");
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(session(
secret: 'super duper secret'
));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded( extended: false ));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(passport.initialize());
app.use(passport.session()); //This creates a unique has for our session
//Initialize Passport
var initPassport = require('./passport-init');
initPassport(passport);
app.use('/auth', authenticate);
app.use('/api', api);
// catch 404 and forward to error handler
app.use(function(req, res, next)
var err = new Error('Not Found');
err.status = 404;
next(err);
);
// error handler
app.use(function(err, req, res, next)
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : ;
// render the error page
res.status(err.status || 500);
res.render('error');
);
module.exports = app;
Authenticate.js
var express = require('express');
var router = express.Router();
module.exports = function(passport)
//sends successful login state back to angular
router.post('/success', function(req, res)
console.log('successful');
res.send(state: 'success', user: req.user ? req.user : null);
);
//sends failure login state back to angular
router.get('/failure', function(req, res)
res.send(state: 'failure', user: null, message: "Invalid username or password");
);
//log in
router.post('/login', passport.authenticate('login', failureRedirect: '/auth/failure' ),
function(req, res)
//res.redirect('/auth/success');
res.send(state: 'success', user: req.user ? req.user : null);
);
//sign up
router.post('/signup', passport.authenticate('signup',
successRedirect: '/auth/success',
failureRedirect: '/auth/failure'
));
//log out
router.get('/signout', function(req, res)
console.log('Signing out...');
req.logout();
res.redirect('/');
);
return router;
【问题讨论】:
【参考方案1】:您在“/”下没有注册任何内容。所以当“auth/logout”调用重定向到redirect(“/”)时,我猜客户端被重定向但进入你的“错误处理程序”,它发送一个500错误......
【讨论】:
-> 尝试重定向到“api/”;如果我了解您的结构,它似乎是您的应用程序的根。 不幸的是,控制台也没有记录注销,所以就好像它根本没有调用该函数。改了还是不行 所以错误可能来自req.logout()
...您确定某种安全措施不会阻止使用 get 查询调用 req.logout 吗?
也许这会帮助***.com/questions/13758207/…
感谢帮助,但仍然没有骰子。再说一次,它甚至没有运行console.log('Signing out...')
,所以我不知道这是否与最初的通话有关。以上是关于router.get 函数没有被调用的主要内容,如果未能解决你的问题,请参考以下文章