-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
80 lines (68 loc) · 1.8 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
/**
* Simple REST App
* Using Express - I can olso use Restify or Hapi
* Created by Alexey S. Kiselev on June 2017.
*/
var express = require('express'),
path = require('path'),
bodyParser = require('body-parser'),
session = require('express-session'),
cookieParser = require('cookie-parser');
// Connecting to Database
var db = require('./config/db.js').url;
var mongoose = require('mongoose');
mongoose.connect(db,function(err) {
if(err) {
console.log(err);
} else {
console.log('Successfully connected to', db);
}
});
var MongoStore = require('connect-mongo')(session);
// Passport Auth
var passport = require('passport');
require('./config/passport')(passport);
// Routes
var index = require('./routes/index'),
user = require('./routes/user');
// Create app
var app = express();
// View Engine
app.set('views',path.join(__dirname,'views'));
app.set('view engine','ejs');
// App Settings
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(session({
secret: 'verysecterstring',
saveUninitialized: true,
resave: false,
cookie : { maxAge: 43200000 },
store: new MongoStore({
url: db,
collection : 'sessions',
ttl: 43200
}),
maxAge: new Date(Date.now() + 43200000)
}));
// Auth with Passport
app.use(passport.initialize());
app.use(passport.session());
// Routes
app.use('/', index);
app.use('/user',user);
// Error and 404
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// Error handler
app.use(function(err, req, res, next) {
// Render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;