正如我正在学习使用NodeJS,Express,MongoDB构建应用程序一样。我正在开发一个用户上传文件的特定网络应用程序。我已成功使用multer将文件保存到uploads文件夹。 (现在我假设它正确保存,因为表单不会在提交时抛出错误,而uploads文件夹有一个带有输入名称和当前日期的文件)。我正在使用passport.js注册新用户并验证现有用户。所以我需要将此文件路径发送到passport.authenticate(本地注册,{/ 此处的成功与错误 /},以便在我的数据库中创建新用户时它具有此信息我可以在个人资料页面上使用它来显示他们上传的图像。下面是必要的文件夹结构(不是全部,但涉及的内容)以及路由,模型(对于db)和passport.js文件。护照文档看起来像只有你可以用passport.authenticate发送的其他东西是会话对象所以我不知道如何实现这一点。任何人都可以解释它是如何做到的?
文件夹结构
myapp
-config
-passport.js
-models
-user.js
-public
-routes
-index.js
-uploads
-stateID-09202016
-views
-signup.ejs
-profile.ejs
index.js(./路由/ index.js)
var express = require('express');
var passport = require('passport');
var multer = require('multer');
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './uploads')
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now())
}
})
var upload = multer({ storage: storage })
var router = express.Router();
router.get('/signup', function(req, res) {
res.render('signup.ejs', { message: req.flash('loginMessage') });
})
.post('/signup', upload.single('stateID'), passport.authenticate('local- signup', {
successRedirect: '/profile',
failureRedirect: '/signup',
failureFlash: true,
}));
module.exports = router;
function isLoggedIn(req, res, next) {
if (req.isAuthenticated())
return next();
res.redirect('/');
}
user.js的(./型号/ user.js的)
var mongoose = require('mongoose');
var bcrypt = require('bcrypt-nodejs');
var userSchema = mongoose.Schema({
local:{
email: String,
password: String,
firstName: String,
mInitial: String,
lastName: String,
phoneNumber: String,
cpassword: String,
stateID: String
},
});
userSchema.methods.generateHash = function(password) {
return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
};
userSchema.methods.validPassword = function(password) {
return bcrypt.compareSync(password, this.local.password);
};
module.exports = mongoose.model('User', userSchema);
passport.js(./配置/ passport.js)
var LocalStrategy = require('passport-local').Strategy;
var User = require('../models/user');
var fs = require('fs');
module.exports = function(passport) {
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
passport.use('local-signup', new LocalStrategy({
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true,
},
function(req, email, password, done) {
process.nextTick(function() {
User.findOne({ 'local.email': email }, function(err, user) {
if (err)
return done(err);
if (user) {
return done(null, false, req.flash('signupMessage', 'That email is already in use.'));
} else {
var newUser = new User();
newUser.local.email = email;
newUser.local.firstName = req.body.firstName;
newUser.local.mInitial = req.body.mInitial;
newUser.local.lastName = req.body.lastName;
newUser.local.phoneNumber = req.body.phoneNumber;
newUser.local.cpassword = req.body.cpassword;
newUser.local.password = newUser.generateHash(password);
newUser.save(function(err) {
if (err)
throw err;
return done(null, newUser);
});
}
});
});
}));
/*Not showing passport.use(local-login) to save space*/
};