我正在尝试将本地身份验证添加到我的node.js应用程序中。在遵循有关https://scotch.io/tutorials/easy-node-authentication-setup-and-local的教程之后,我遇到了错误:“ TypeError:app.use()需要中间件功能”。
我认为这与我的app.js文件中的app.use(indexRoutes)有关,但是我不确定如何解决此问题?任何帮助将不胜感激。
到目前为止,这是我的代码: app.js:
var express = require('express'),
session = require("express-session"),
bodyParser = require('body-parser'),
app = express().use(bodyParser.json()), //creates express http server
passport = require('passport'),
sanitizer = require('express-sanitizer'),
mongoose = require("mongoose"),
cookieParser = require('cookieparser'),
dotnev = require('dotenv').config(),
https = require('https'),
flash = require('connect-flash'),
fs = require('fs'),
config = require('./config/config'),
_ = require("underscore");
require('./config/passport')(passport);
var indexRoutes = require("./routes/index")(app, passport);
app.use(bodyParser.urlencoded({extended: true}));
app.use(sanitizer());
app.use(express.static(__dirname + "/public")); //tells express to serve the contents of the public directory
app.use(cookieParser);
//no longer need to specify that the view files are .ejs
app.set("view engine", "ejs");
app.use(session({
secret: "akjjkjnisaiuu8998323jdkadsih892rhoisdfasl",
resave: true,
saveUninitialized: true,
cookie: {
maxAge: 1200000
}
}));
app.use(passport.initialize);
app.use(passport.session());
app.use(flash());
app.use(indexRoutes);
mongoose.connect(process.env.MONGOLAB_URL, { useNewUrlParser: true });
index.js:
var express = require("express"),
router = express.Router(),
_ = require("underscore"),
User = require("../models/user"),
auth = require("../routes/auth"),
config = require('../config/config'),
freshbooks = require("../modules/freshbooks"),
quickbooks = require("../modules/quickbooks");
module.exports = function(router, passport){
//------------------------------------//
//***------------ROUTES------------***//
//------------------------------------//
router.get("/", function(req, res) {
res.render("landing");
});
router.get("/register", auth.optional, function(req, res) {
res.render("register");
});
router.get("/login", auth.optional, function(req, res) {
res.render("login");
});
router.get("/admin", isLoggedIn, function(req, res) {
res.render('admin', {
user : req.user // get the user out of session and pass to template
});
});
//------------------------------------//
//***-------------AUTH-------------***//
//------------------------------------//
router.post('/register', passport.authenticate('local-signup', {
successRedirect : '/admin', // redirect to the secure profile section
failureRedirect : '/register', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}));
router.post('/login', passport.authenticate('local-login', {
successRedirect : '/profile', // redirect to the secure profile section
failureRedirect : '/login', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}));
function isLoggedIn(req, res, next) {
// if user is authenticated in the session, carry on
if (req.isAuthenticated())
return next();
// if they aren't redirect them to the home page
res.redirect('/');
}
router.get('/logout', function(req, res) {
req.logout();
res.redirect('/');
});
}
在此先感谢您的帮助!
答案 0 :(得分:0)
在app.js中
$user_name
和index.js
var { registerIndexRoutes } = require("./routes/index");
var indexRoutes = registerIndexRoutes(router, passport);
app.use(indexRoutes);
它将为您提供帮助。
答案 1 :(得分:0)
由于该功能module.exports = function(router, passport){
不是有效的中间件,因此出现错误。
来自express docs:中间件函数是可以访问请求对象(req),响应对象(res)和应用程序请求-响应周期中的下一个函数的函数。
语法如下:
function(req, res, next) {
// Implement the middleware function based on the options object
next()
}
req
:中间件功能的HTTP响应参数,按照惯例称为“ res”。
res
:中间件函数的HTTP请求参数,按照惯例称为“ req”。
next
:中间件函数的回调参数,按照惯例称为“ next”。
调用此函数时,function(router, passport){
:
var indexRoutes = require("./routes/index")(app, passport);
indexRoutes
,包含function(router, passport)
的返回值,该返回值不返回中间件函数(返回undefined
)。
两种解决问题的方法:
更改您的导入:
// remove this line and put the below instead
app.use(cookieParser);
// and modify and move your import
require("./routes/index")(app, passport);
使用Express router
:将您的index.js
更改为
// note your exports it's not a function, you're exporting this **router** object -> router = express.Router(),
module.exports = router;
//------------------------------------//
//***------------ROUTES------------***//
//------------------------------------//
router.get("/", function(req, res) {
res.render("landing");
});
router.get("/register", auth.optional, function(req, res) {
res.render("register");
});
...
并将导入更改为:
var indexRoutes = require("./routes/index");