-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
145 lines (117 loc) · 3.66 KB
/
app.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
134
135
136
137
138
139
140
141
142
143
144
145
var express = require('express');
const nodemailer = require('nodemailer');
const xoauth2 = require('xoauth2');
var multer = require('multer')
var bodyParser = require('body-parser')
const mongodb = require('mongodb');
var passport = require('passport');
const passportConfig = require('./config/passport');
const cookieSession = require('cookie-session');
const keys = require('./config/auth');
const nconf = require('nconf');
const storage = require('./storage')
nconf.argv().env().file('keys.json');
var upload = multer({ dest: 'insert_book/' });
const user = nconf.get('mongoUser');
const pass = nconf.get('mongoPass');
const host = nconf.get('mongoHost');
const port = nconf.get('mongoPort');
const authCheck = (req, res, next) => {
if (!req.user) {
console.log('not sign in')
res.redirect('/sign_in');
} else {
console.log('have been sign in')
next();
}
};
const createServer = () => {
var app = express();
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
// Viewing engine
app.set('view engine', 'ejs');
var routes = require('./routes');
var path = require('path');
app.use(express.static(path.join(__dirname, 'public')));
// set up session cookies
app.use(cookieSession({
maxAge: 24 * 60 * 60 * 1000,
keys: [keys.session.cookieKey]
}));
// initialize passport
app.use(passport.initialize());
app.use(passport.session());
// Routes
// Home Route
app.get('/', routes.home);
app.get('/page/:page?', routes.home);
app.get('/success', passport.authenticate('google'), (req, res) => {
res.redirect('/');
});
app.get('/book_id/:id?', routes.book_single);
app.get('/sign_in', routes.sign_in);
app.get('/sign_out', (req, res) => {
req.logOut();
res.redirect('/');
});
app.get('/send_mail', authCheck, routes.send_mail);
app.post('/send_mail_res', routes.sendEmail);
app.get('/profile', authCheck, routes.profile);
app.get('/add_book', authCheck, routes.add_book);
app.get('/edit_book/:id?', authCheck, routes.edit_book);
app.get('/delete_book/:id?', authCheck, routes.delete_book);
app.post('/insert_book', upload.array('bookImages', 2), routes.insertBook);
app.post('/update_book/:id?', upload.array('bookImages', 2), routes.updateBook);
app.get('/auth/google', passport.authenticate('google', {
scope: [
'https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/userinfo.email',
'https://mail.google.com/',
'https://www.googleapis.com/auth/gmail.modify',
'https://www.googleapis.com/auth/gmail.compose',
'https://www.googleapis.com/auth/gmail.send'
],
accessType: 'offline',
prompt: 'consent'
}));
app.get('/favicon.ico', routes.notfound);
app.get('*', routes.notfound);
const PORT = process.env.PORT || 8080;
app.listen(PORT, function () {
console.log('App server running on localhost: ', PORT)
});
}
// [START client]
let uri = `mongodb://${user}:${pass}@${host}:${port}`;
if (nconf.get('mongoDatabase')) {
uri = `${uri}/${nconf.get('mongoDatabase')}`;
}
console.log('connecting to:', uri);
try {
mongodb.MongoClient.connect(uri, (err, db) => {
if (err) {
console.log('can not connect to database with error: ', err);
} else {
console.log('connected to database');
}
var books;
db.collection('books').find({}).toArray(function (error, data) {
if (error) {
console.log('get books err', error);
} else {
const len = data.length || 0;
storage.mongo = db;
storage.len = len;
storage.pageCount = Math.ceil(len / 6);
console.log('length', Math.ceil(len / 6));
createServer();
}
})
});
} catch (error) {
console.log('error: ', error);
createServer();
}