-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
136 lines (122 loc) · 5.03 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
134
135
136
var express = require('express');
var path = require('path');
var logger = require('morgan');
var compression = require('compression');
var methodOverride = require('method-override');
var session = require('express-session');
var flash = require('express-flash');
var bodyParser = require('body-parser');
var expressValidator = require('express-validator');
var dotenv = require('dotenv');
var exphbs = require('express-handlebars');
var passport = require('passport');
var Data_file = require('./models/Data_file.js');
// Load environment variables from .env file
dotenv.load();
// Controllers
var HomeController = require('./controllers/home');
var userController = require('./controllers/user');
var contactController = require('./controllers/contact');
var ocrController = require('./controllers/ocr');
var upeditController = require('./controllers/upedit');
// Passport OAuth strategies
require('./config/passport');
var app = express();
var hbs = exphbs.create({
defaultLayout: 'main',
helpers: {
ifeq: function(a, b, options) {
if (a === b) {
return options.fn(this);
}
return options.inverse(this);
},
toJSON : function(object) {
return JSON.stringify(object);
}
}
});
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
app.set('port', process.env.PORT || 3000);
app.use(compression());
app.use(logger('dev'));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true }));
app.use(bodyParser.json({limit: '50mb'}));
app.use(expressValidator());
app.use(methodOverride('_method'));
app.use(session({ secret: process.env.SESSION_SECRET, resave: true, saveUninitialized: true }));
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
app.use(function(req, res, next) {
sendMeData_file(req.user, function(data_file) {
res.locals.user = req.user ? req.user.toJSON() : null;
if (data_file) {
res.locals.data_file = data_file ? data_file.toJSON() : null;
}
next();
});
});
function sendMeData_file(user, callback) {
if (!user) {
callback();
} else {
new Data_file({})
.query(function(qb) {
qb.where('id_user', '=', user.id)
.orderBy('created_at', 'DESC');
})
.fetchAll()
.then(function(data_file) {
callback(data_file);
});
}
}
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', HomeController.index);
app.get('/contact', contactController.contactGet);
app.post('/contact', contactController.contactPost);
app.get('/account', userController.ensureAuthenticated, userController.accountGet);
app.put('/account', userController.ensureAuthenticated, userController.accountPut);
//WIP routes
app.post('/home', userController.ensureAuthenticated, HomeController.homePost);
app.put('/home', userController.ensureAuthenticated, HomeController.homePut);
app.delete('/home', userController.ensureAuthenticated, HomeController.homeDelete);
app.get('/upbrowser', userController.ensureAuthenticated, HomeController.upbrowserGet);
app.get('/OCR', userController.ensureAuthenticated, ocrController.ocrGet);
app.post('/OCR', userController.ensureAuthenticated, ocrController.ocrPost);
app.get('/Upedit', userController.ensureAuthenticated, upeditController.upeditGet);
app.post('/Upedit', userController.ensureAuthenticated, upeditController.upeditPost);
app.post('/upbrowserSave', userController.ensureAuthenticated, HomeController.upbrowserSave);
app.delete('/account', userController.ensureAuthenticated, userController.accountDelete);
app.get('/signup', userController.signupGet);
app.post('/signup', userController.signupPost);
app.get('/login', userController.loginGet);
app.post('/login', userController.loginPost);
app.get('/forgot', userController.forgotGet);
app.post('/forgot', userController.forgotPost);
app.get('/reset/:token', userController.resetGet);
app.post('/reset/:token', userController.resetPost);
app.get('/logout', userController.logout);app.get('/unlink/:provider', userController.ensureAuthenticated, userController.unlink);
app.get('/auth/facebook', passport.authenticate('facebook', { scope: ['email', 'user_location'] }));
app.get('/auth/facebook/callback', passport.authenticate('facebook', { successRedirect: '/', failureRedirect: '/login' }));
app.get('/auth/google', passport.authenticate('google', { scope: 'profile email' }));
app.get('/auth/google/callback', passport.authenticate('google', { successRedirect: '/', failureRedirect: '/login' }));
app.get('/auth/twitter', passport.authenticate('twitter'));
app.get('/auth/twitter/callback', passport.authenticate('twitter', { successRedirect: '/', failureRedirect: '/login' }));
// Production error handler
if (app.get('env') === 'production') {
app.use(function(err, req, res, next) {
console.error(err.stack);
res.sendStatus(err.status || 500);
});
}
app.use(function(req, res, next){
res.setHeader('Content-Type', 'text/plain');
res.status(404).send('Page introuvable !');
});
app.listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'));
});
module.exports = app;