-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
133 lines (91 loc) · 3.26 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* Created by anshulmittal on 24/7/17.
*/
const express = require('express');
const app = express();
const passport = require('./config/passport');
const bp = require('body-parser');
const session = require('express-session');
const path = require('path');
const db = require('./config/db');
//To create an express session
app.use(session({
secret: 'somesecret',
resave: false,
saveUninitialized: true
}));
//To parse the passed data
app.use(bp.json());
app.use(bp.urlencoded({extended: true}));
//This initialises Passport js
app.use(passport.passport.initialize());
app.use(passport.passport.session());
//This is used to set engine to process and render the .ejs files
app.set('view engine','ejs');
//Accessing the main/root of the website
app.get('/',(req,res)=>{
let userb=req.user;
// console.log("Data at /:");console.log(userb);
res.render('index',{user:req.user});
});
//Accessing Login Page
app.get('/login',(req,res)=>{
res.render('login');
});
//To access profile
app.get('/profile',(req,res)=>{
let userb=req.user;
// console.log("Data at /profile:");console.log(userb);
res.render('profile',{//appid:process.env.CLIENT_ID,
user:userb,postsdata: 'NULL'});
});
//To logout
app.get('/logout', function(req, res){
req.logout();
res.redirect('/');
});
app.use('/public',express.static(path.join(__dirname,'public')));
app.get('/auth/facebook', passport.passport.authenticate('facebook'));
app.get('/auth/facebook/callback',
passport.passport.authenticate('facebook', { successRedirect: '/',
failureRedirect: '/login' }));
let x;
//Array created to store all the promises of the posts
var promises=[];
//Here req is sent from the Profile.ejs to fetch all the posts
app.get('/getpostfrompage',(req,res,next)=>{
passport.FB.api("/126976547314225/posts?fields=shares,likes,message,full_picture,created_time",function (response) {
//console.log("entered getpostsfrompage");
if (response && !response.error) {
// console.log("response exists");
// console.log(response);
for(let i in response.data){
// console.log("logging response.data for :"+i);
promises.push(db.insertpost(response.data[i]));
}
//This runs after all the posts have been stored,Thus there is no loss of data
Promise.all(promises).then(()=>{
// console.log("Now calling and dsiplaing from db");
db.displaypost().then((data)=>{
x=data;
// console.log("Entered displaypost");
// console.log(x);
res.render('profile', {user: req.user, postsdata: x});
}).catch((err)=>{
console.log("Error");
console.log(err);
});
}).catch((err)=>{
console.log("Error in promises");
console.log(err);
})
}
}
);
});
app.use((req,res,next)=>{
res.send("Error : 404 Page Requested Not Found")
});
app.listen(5400, function () {
console.log("Server started on http://localhost:5400");
});