-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (59 loc) · 1.91 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
const dotenv = require(`dotenv`);
const express = require(`express`);
const bodyParser = require(`body-parser`);
const hbs = require(`hbs`);
const session = require('express-session');
const routes = require('./routes/routes.js');
const db = require(`./models/db.js`);
const MongoDBStore = require('connect-mongodb-session')(session);
// Set-up Express
const app = express();
// Set-up DotEnv
dotenv.config();
port = process.env.PORT;
hostname = process.env.HOSTNAME;
// connect to database
db.connect();
// store for session
const store = new MongoDBStore({
uri: 'mongodb+srv://aianfuentespina:[email protected]/?retryWrites=true&w=majority', // Your MongoDB connection URI
collection: 'sessions', // Collection name to store sessions
});
// setup sessions system
app.use(
session({
secret: 'Your_Secret_Key',
resave: false,
saveUninitialized: false,
store: store,
cookie: { maxAge: 60 * 60 * 1000 }, // Session duration in milliseconds (e.g., 1 hour)
})
);
// Set handlebars as view engine
app.set(`view engine`, `hbs`);
// Use this to call session variables using
// {{session.VARIABLE}} in handlebars
app.use(function (req, res, next) {
res.locals.session = req.session;
next();
});
hbs.registerPartials(__dirname + '/views/partials');
// HBS helper
// if equals
hbs.registerHelper('ifeq', function (a, b, options) {
if (a == b) { return options.fn(this); }
return options.inverse(this);
});
hbs.registerHelper('ifnoteq', function (a, b, options) {
if (a != b) { return options.fn(this); }
return options.inverse(this);
});
// Set-up public folder and routes
app.use(express.static(`public`));
app.use(bodyParser.urlencoded( {extended: false} ))
app.use(`/`, routes);
// Server response for confirmation
app.listen(port, function() {
console.log(`Server running at: `);
console.log(`http://` + hostname + `:` + port);
});