快递将护照发送至app.use(路线)

时间:2016-08-17 22:11:29

标签: javascript node.js express passport.js mean-stack

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

Server.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 个答案:

答案 0 :(得分:2)

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

<ImageButton
android:id="@+id/buttomMenuBack"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="35dp"
android:text="Back"
android:background="@drawable/back_button"
android:onClick="onClickButtomMenu"/>

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

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

由于app.use('/api', profile(passport)); 返回快速路由器对象。