我正在使用快递构建应用程序,用户只能看到他所关注的用户的帖子。 Passport用于用户身份验证。我面临的问题是,当用户关注新用户时,除非他退出然后再次登录,否则他无法看到新关注用户的帖子。
起初我认为问题是由于数据库同步不正确而引起的,但现在我猜这是因为passport.js的使用不正确。
Server.js
/**
* Created by harshitbisht96 on 26/7/17.
*/
const express=require('express');
const fs=require('fs');
const path=require('path');
const bp = require('body-parser');
const session = require('express-session')
const cp = require('cookie-parser');
var hbs = require('hbs');
var ejs = require('ejs');
var db=require('./database.js');
const app=express();
const multer=require('multer');
const mongo=require('mongodb');
const homeRoute=require('./routes/home');
const loginRoute=require('./routes/login');
const registerRoute=require('./routes/register');
const passport=require('./passport')
const profileRoute=require('./routes/profile')
app.use(cp('somesecret'));
app.use(session({
secret: 'somesecret',
resave: false,
saveUninitialized: true
}));
app.use(bp.urlencoded({extended: true}))
app.use(bp.json())
app.use(passport.initialize());
app.use(passport.session());
app.set('view engine', 'hbs');
// app.set('view engine', 'html');
app.set('views', __dirname + '/views');
// app.engine('html', require('ejs').renderFile);
app.use('/posts/',express.static(__dirname + '/userImages'));
app.use('/posts/',express.static(__dirname + '/uploads'));
app.use('/profile/',express.static(__dirname + '/userImages'));
app.use(express.static(__dirname + '/uploads'));
app.use(express.static(__dirname + '/bootstrap'));
console.log(__dirname + '/uploads/');
// const Grid=require('gridfs-stream');
var file2upload,fileName,filePath;
var userImageFileName
// const db = new mongo.Db('gridDb', new mongo.Server("127.0.0.1", 27017));
// const gfs = Grid(db, mongo);
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './uploads/');
},
filename: function (req, file, cb) {
file2upload=file.originalname;
fileName=file.fieldname + '-' + Date.now()+'.'+file.mimetype.toString().split("/")[1];
cb(null, fileName);
filePath=path.join(__dirname,'uploads/');
filePath+=fileName;
}
});
var userImageStorage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './userImages/');
},
filename: function (req, file, cb) {
file2upload=file.originalname;
userImageFileName=file.fieldname + '-' + Date.now()+'.'+file.mimetype.toString().split("/")[1];
cb(null, userImageFileName);
filePath=path.join(__dirname,'userImages/');
filePath+=fileName;
}
});
filePath=path.join(__dirname,'uploads/');
filePath+=fileName;
var upload = multer({ storage: storage }).single('avatar')
var uploadUserImage = multer({ storage: userImageStorage }).single('avatar');
app.use(bp.json());
app.use(bp.urlencoded());
app.use(cp());
// gfs.files.find({ aliases: 2 }).toArray(function (err, files) {
// if (err) {
// console.log(err);
//
// }
// console.log(files);
//
// });
function checkLoggedIn(req, res, next) {
if (req.user) {
// res.redirect('/posts');
next();
} else {
res.status(404).send('Unauthorised')
}
}
app.get('/', function(req,res,next){
res.header('Cache-Control', 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0');
if(req.user){
res.redirect('/posts');
}
else{
next();
}
});
app.use('/profile',profileRoute)
app.use('/posts',checkLoggedIn,homeRoute);
app.use('/register',registerRoute);
app.use('/',loginRoute);
app.post('/upload', function (req, res) {
upload(req, res, function (err) {
if (err) {
// An error occurred when uploading
console.log(err);
return;
}
db.then(function(data){
var postStory=req.body.story;
var imageCollection=data.collection('images');
imageCollection.insert({"image":fileName,"story":postStory,"postedBy":req.user.username,"postedByImage":req.user.image}).then(function(){
console.log("Success");
})
});
res.redirect('/posts');
})
});
app.post('/users', function (req, res) {
uploadUserImage(req, res, function (err) {
if (err) {
// An error occurred when uploading
console.log(err);
return;
}
db.then(function(data){
var userCollection=data.collection('users');
userCollection.insert({"firstname": req.body.firstname, "lastname": req.body.lastname, "age":req.body.age, "city":req.body.city, "username":req.body.username, "password":req.body.password, "description":req.body.description, "image":userImageFileName,"followers":[],"following":[]}).then(function(){
})
});
res.redirect('/');
})
});
app.post('/comment',function(req,res){
// console.log(req.body.postId);
// console.log(req.body.comment)
db.then(function(data){
var commentsCollection=data.collection('comments');
var newComment=({body:req.body.comment,post:req.body.postId,by:req.user.username});
commentsCollection.insert(newComment).then(function(){
console.log("comment added");
commentsCollection.find({}).toArray().then(function(data){
console.log(data.length)
})
})
});
res.redirect('/posts/'+req.body.postId);
})
//Problem in this part of the code
app.post('/addFollower',function(req,res){
console.log(req.user);
db.then(function(data){
var userCollection=data.collection('users');
userCollection.update({"username":req.user.username},{$addToSet:{"following":req.body.followWho}});
userCollection.update({"username":req.body.followWho},{$addToSet:{"followers":req.user.username}});
console.log("User followed");
console.log(req.user)
res.redirect('/posts')
});
});
// res.send("Yo")
app.post('/login', passport.authenticate('local', {
failureRedirect: '/',
successRedirect: '/posts',
}),
);
app.use('/',function(req,res){
res.send("Page not found")
})
// passport.authenticate('local', { failureFlash: 'Invalid username or password.' });
// passport.authenticate('local', { successFlash: 'Welcome!' });
app.listen(1111,function(){
console.log("app running at http://localhost:1111");
});
Database.js
var mongodb=require('mongodb');
var MongoClient=mongodb.MongoClient;
var url='mongodb://localhost:27017/imageDatabase';
function getDb(){
return MongoClient.connect(url).then(function (db) {
// console.log(MongoClient.connect(url));
return db;
})
}
module.exports=getDb();