forked from phenax/project-venus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
96 lines (57 loc) · 2.23 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
const express= require('express');
const app= express();
require('./config/express')(app, __dirname);
const sequleize= require('./config/db')();
// Models
const db= { models: {}, sequleize };
db.models.User= require('./models/User')(sequleize);
db.models.Page= require('./models/Page')(sequleize);
db.models.Review= require('./models/Review')(sequleize);
db.models.Category= require('./models/Category')(sequleize);
// Models Relations
db.models.Category.hasMany(db.models.Page, { foreignKey: 'categoryId' });
db.models.Page.belongsTo(db.models.Category);
db.models.Review.belongsTo(db.models.Page);
db.models.Page.hasMany(db.models.Review, { foreignKey: 'pageId' });
db.models.User.hasMany(db.models.Review, { foreignKey: 'userId' });
db.models.Review.belongsTo(db.models.User);
// OAuth config
require('./config/auth/passport')(db);
// Controllers
const IndexCtrl= require('./controllers/IndexController');
const UsersCtrl= require('./controllers/UsersController');
const ReviewsCtrl= require('./controllers/ReviewsController');
const indexController= new IndexCtrl(app, db);
const usersController= new UsersCtrl(app, db);
const reviewsController= new ReviewsCtrl(app, db);
// Routes
// Middleware to redirect if the user isnt logged in
const isLoggedIn=
(req, res, next) => (req.isAuthenticated())? next(): res.redirect('/');
app.get('/', indexController.home);
app.get('/auth/google', usersController.authenticate);
app.get('/auth/google/callback', usersController.authCallback, usersController.redirect);
app.get('/me', isLoggedIn, usersController.profile);
app.get('/user/:user_id', usersController.profile);
app.get('/logout', usersController.logout);
app.get('/api/reviews/add', reviewsController.addReview);
app.get('/api/pages/search', reviewsController.search);
app.get('/category/:category', reviewsController.category);
app.get('/:category?/:subject?', reviewsController.page);
// Error handlers
// If err, next err
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
if (app.get('env') === 'development') {
app.use(function(err, req, res) {
res.status(err.status || 500);
res.render('ErrorPage', {
message: err.message,
error: err
});
});
}
module.exports= app;