快递发送护照到 app.use(route)

Posted

技术标签:

【中文标题】快递发送护照到 app.use(route)【英文标题】:Express send passport to app.use(route) 【发布时间】:2016-12-24 17:25:27 【问题描述】:

我正在尝试将护照发送到 app.use('/api')(passport) 但这样做时似乎出现错误。我应该把它发送到 app.use() 还是我可以在 profile.js 文件中 recure() 它?我是新来表达的,并且正在关注如何实现 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'));
);

backend.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;

routes.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;

profile.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;
;

【问题讨论】:

【参考方案1】:

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

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

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

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

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

【讨论】:

我现在觉得有点傻,个人资料(护照)有效。感谢您的帮助!

以上是关于快递发送护照到 app.use(route)的主要内容,如果未能解决你的问题,请参考以下文章

什么是passport.initialize()? (nodejs快递)

节点,快递 - 无法获取路线

在护照 app.use() 回调中未收到 Req 和 Res 参数

Laravel 护照(看不懂)

节点、快递、护照 - 更新记录后更新会话

使用 Passport 进行社交身份验证登录后如何重定向到个人资料页面? (角度,快递,护照,jwt)