-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (47 loc) · 1.69 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
require('dotenv').config();
// import module `express`
const express = require('express');
// import module `hbs`
const hbs = require('hbs');
// import module `express-session`
const session = require('express-session');
// import module `mongoose`
const mongoose = require('mongoose');
// import module `connect-mongo`
const MongoStore = require('connect-mongo')(session);
// import module `routes` from `./routes/routes.js`
const routes = require('./routes/routes.js');
// import module `database` from `./model/db.js`
const db = require('./models/db.js');
const app = express();
if(process.env.PORT) var PORT = process.env.PORT;
else var PORT = 9090;
const host = '0.0.0.0';
// set `hbs` as view engine
app.set('view engine', 'hbs');
// sets `/views/partials` as folder containing partial hbs files
hbs.registerPartials(__dirname + '/views/partials');
// parses incoming requests with urlencoded payloads
app.use(express.urlencoded({limit: '50mb', extended: true}));
// set the folder `public` as folder containing static assets
// such as css, js, and image files
app.use(express.static('public'));
// connects to the database
db.connect();
// use `express-session`` middleware and set its options
// use `MongoStore` as server-side session storage
app.use(session({
'secret': 'kahit-ano',
'resave': false,
'saveUninitialized': false,
store: new MongoStore({mongooseConnection: mongoose.connection})
}));
// define the paths contained in `./routes/routes.js`
app.use('/', routes);
app.use(function (req, res) {
res.render('error');
});
// binds the server to a specific port
app.listen(PORT, host, function () {
console.log(`Kahit-Ano now running at: http://localhost:${PORT}`);
});