-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
47 lines (42 loc) · 1.63 KB
/
server.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
//import the necessary modules
const path = require('path');
const express = require('express');
const hbs = require('hbs');
const handlebars_helper = require(`./views/hbs-helper.js`);
const routes = require('./routes/routes.js')
const mongoose = require('mongoose');
const db = require('./models/db.js');
const session = require('express-session');
const MongoStore = require('connect-mongo');
const app = express();
process.env.HOSTNAME = "localhost"
process.env.PORT = "3000"
process.env.DB_URL = "mongodb+srv://admin:[email protected]/jaelle-residences-db?retryWrites=true&w=majority"
//parse incoming requests with urlencoded payloads
app.use(express.urlencoded({extended: false}));
//set the file path containing the static assets
app.use(express.static(path.join(__dirname, 'public')));
//set the session middleware
app.use(
session({
secret: 'jaelle-residences',
resave: 'false',
saveUninitialized: 'false',
store: MongoStore. create({mongoUrl: process.env.DB_URL})
})
);
//set hbs as the view engine
app.set('view engine', 'hbs');
//set the file path containing the hbs files
app.set('views', path.join(__dirname, 'views'))
//set the file path of the paths defined in './routes/routes.js'
app.use('/', routes);
//set the file path containing the partial hbs files
hbs.registerPartials(path.join(__dirname, 'views/partials'));
//connect to the database
db.connect();
//bind the server to a port and a host
app.listen(process.env.PORT, process.env.HOSTNAME, function() {
console.log(`Server is running at http://${process.env.HOSTNAME}:${process.env.PORT}`);
});
module.exports = app;