-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
85 lines (71 loc) · 2.4 KB
/
index.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
const dotenv = require('dotenv');
const dotenvExpand = require('dotenv-expand');
// configure environment variables
const myEnv = dotenv.config();
// expand existing env variables
dotenvExpand(myEnv);
const express = require('express'),
bodyParser = require('body-parser'),
path = require('path'),
debug = require('debug')('SERVER'),
passport = require('passport'),
session = require('express-session'),
flash = require('connect-flash'),
cloudinary = require('cloudinary').v2;
const app = express();
// require model
require('./models/user.model');
require('./models/source.model');
const userRoute = require('./routes/user.route'),
authRoute = require('./routes/auth.route'),
sourceRoute = require('./routes/source.route'),
adminRoute = require('./routes/admin.route');
const auth = require('./middlewares/auth.middleware'),
requrestMiddleware = require('./middlewares/request.middleware');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(express.static('public'));
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
cookie: {
expires: 120000
}
}));
app.use('/node_modules', express.static(path.join(__dirname, '/node_modules')));
// connect to mlab cloud database
require('./config/mongodb.config').connectToLocalhost();
// configure cloudinary to upload file
require('./config/cloudinary.config')(cloudinary);
require('./config/passport.config')(passport);
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
app.set('view engine', 'pug');
app.set('views', path.join(__dirname, '/views'));
// Wire request 'pre' actions
app.use(requrestMiddleware.wirePreRequest);
app.get('/', (req, res) => {
res.render('index');
});
// using api
app.use('/api', auth.requireAuth, require('./api'));
app.use('/users', auth.requireAuth, userRoute);
app.use('/auth', authRoute);
app.use('/sources', sourceRoute);
app.use('/admin',
// auth.requireAuth,
// auth.requireRole([ 'admin' ]),
adminRoute
);
// Since not found any middleware
app.use(requrestMiddleware.notFoundMiddleware);
// Wire request 'post' actions
// any error
app.use(requrestMiddleware.wirePostRequest);
const PORT = process.env.PORT;
const server = app.listen(PORT, function () {
debug(`Server is now running on http://localhost:${server.address().port}`);
debug('='.repeat(100));
});