-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
78 lines (63 loc) · 2.27 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
// init packages
const express = require('express');
const exphbs = require('express-handlebars');
const session = require('express-session');
const MongoStore = require('connect-mongo');
const flash = require('connect-flash');
const bodyParser = require('body-parser');
const { envPort, sessionKey, dbURL } = require('./config');
const router = require("./routes/routes")
const app = new express();
// init server port
//const port = 3000;
var PORT = envPort || 3000;
var server = app.listen(PORT, function () {
console.log("Listening at port " + PORT + "...");
});
// MONGOOSE stuff
// 'mongodb://localhost/expenseTrackerDB' local url
// const databaseURL = "mongodb+srv://admin:[email protected]/expenseTrackerDB?retryWrites=true&w=majority";
const options = {
useNewURLParser: true,
useUnifiedTopology: true
}
const mongoose = require('mongoose');
mongoose.connect(dbURL, options);
// Initialize data and static folder
app.use(express.json());
app.use(express.urlencoded({ extended: true })) // might change later
app.use(express.static(__dirname + "/public")); // place all html, js, css for the pages here
// using handlebars
app.set('view engine', 'hbs');
app.engine("hbs", exphbs.engine({
extname: "hbs",
helpers: require(__dirname + '/public/hbs-helpers/helpers.js')
}));
//SESSION STUFF
/*
* secret - signs session ID; should be a random string
* store - where sessions get stored
* resave - when false: only saves the session when a value is modified
* saveUninitialized: forces a new but unmodified session to be saved to the store
* cookie: settings for current session cookie
*/
// mongoUrl: 'mongodb://localhost/expenseTrackerDB'
app.use(session({
secret: sessionKey,
store: MongoStore.create({ mongoUrl: dbURL }),
resave: false,
saveUninitialized: true,
cookie: {secure: false, maxAge: 1000 * 60 * 60 * 24 * 7}
}));
//FLASH
app.use(flash());
app.use((req, res, next) => {
res.locals.success_msg = req.flash('success_msg');
res.locals.error_msg = req.flash('error_msg');
next();
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use('/', router);